save_group.php
<?php
include('../dbconnection.php');
$ruser = $_POST['r_user'];
$pid = $_POST['p_id'];
$conn->query("INSERT INTO `new_r_login_p_id` VALUES ('', '$pid', '$ruser')");
?>
group_validator.php
<?php
include('../dbconnection.php');
$ruser = $_POST['r_user'];
$pid = $_POST['p_id'];
$query = $conn->query("SELECT * FROM `new_r_login_p_id` WHERE `r_user` = '$ruser' && `p_id` = '$pid'");
$validate = $query->num_rows;
if($validate > 0){
echo "Success";
}else{
echo "Error";
}
?>
script.js
$(document).ready(function() {
$error = $('<center><label class = "text-danger">Please Fill up the form!</label></center>');
$load_status = $('<center><label class = "text-success">Waiting...</label></center>');
$group_valid = $('<center><label class = "text-danger">Member already joined!</label></center>');
$('#save_group').click(function(){
$error.remove();
$group_valid.remove();
$ruser = $('#usermember').val();
$pid = $('#project').val();
alert("You had me at hello");
if($ruser == "option" ){
$error.appendTo('#loading');
}else{
$load_status.appendTo('#loading');
setTimeout(function(){
console.log("group_validator got loaded");
$.post('group_validator.php', {r_user : $ruser, p_id : $pid},
function(result){
if(result == "Success"){
alert("LOADED");
console.log("$.post call done");
console.log("result = " + result);
$group_valid.appendTo('#loading');
}else{
$.ajax({
type: 'POST',
url: 'save_group.php',
data: {r_user : $ruser, p_id : $pid},
success: function(){
window.location = 'group.php?pid=' + $pid;
}
});
}
}
)
$load_status.remove();
}, 3000);
}
});
});
group.php
<div class="col-sm-4 text-white text-center mt-5">
<div class="card box p-2 text-white mb-3">
<div class="card-header bg-primary text-white">User(s)</div>
<div class="card-body">
<form id="form" method="POST">
<div class="form-group" style="color:#5777ba;">
<label for="inputprojectuser" style="color:#5777ba;">Choose any one:</label>
<select id="usermember" class="form-control chosen-select" data-style="btn-primary" >
<option value="option">Select a member</option>
<?php
$sqli = mysqli_query($conn, "SELECT r_login_id, r_name FROM userregistration_db WHERE r_select = 'user'");
while ($row = mysqli_fetch_array($sqli)) {
echo "<option value=" . $row['r_login_id'] . ">" . $row['r_name'] . "</option>";
}
?>
</select>
<input type="hidden" id="project" value="<?php echo $c_fetch['p_id'] ?>" >
</div>
</div>
</div>
</div>
<div class="col-sm-12 mb-5">
<button type="button" id="save_group" class="btn btn-primary btn-large btn-block">Add User(s)</button>
</div>
</form>
What I have tried:
I've tried add this code
group_validator.php
setTimeout(function() {
$.post('group_validator.php', { r_user: $ruser,r_developer: $rdeveloper, r_pmanager: $rpmanager, p_id: $pid },
function(result) {
console.log("$.post call done");
console.log("result = " + result);
});
$load_status.remove();
}, 3000);
It seems to be gives this in console
$.post call done
script.js:28 result = connect
Success
when it checks the user in the database
script.js:27 $.post call done
script.js:28 result = connect
Error
when the user is not added in the database
I have spended way too much time , i just need to complete this as soon as possible
Guys please help me out
Is there something i'm missing