I'd appreciate any suggestions to get this resolved.
I am using a JS function checkAvailability() to confirm if a group is available for addition to a database. (For some reasons, database key constraints were not applied) While doing this, sometimes my function returns even before receiving the output from the PHP file.
So I get a mixed result. When the output from PHP file is received, it shows the correct result. But rest of the times it just returns false.
Any pointers on how this can be fixed? The code follows:
Thanks, Vish
//function to check groupname availability
function checkAvailability(){
var grpname = $('#groupname').val();
var isAvailable = false
//use ajax to run the check
$.post("checkGroupName.php", { gname: grpname },
function(result){
if(Number(result) == 0){
//show that the groupname is available
isAvailable = true ;
$('#username_availability_result').html(grpname + ' is Available');
}
else{
//show that the groupname is NOT available
isAvailable = false ;
$('#username_availability_result').html(grpname + ' is already taken');
}
});
alert("isAvailable : "+isAvailable);
return isAvailable ;
}
checkGroupName.php file
$gname = $_POST['gname'];
$query = "SELECT * FROM groups WHERE group_name='$gname'";
$result = mysql_query($query);
if(mysql_num_rows($result)){
echo 1;
else
echo 0;