duanan1228 2011-07-28 08:28
浏览 51

在Javascript中按顺序执行命令

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;
  • 写回答

3条回答 默认 最新

  • dopzc64662 2011-07-28 08:32
    关注

    the problem is that $.post sends the request asynchronously and so the rest of the code is executed before the response arrives from the server.

    the solution is to use $.ajax() and to set async option to false so that the call is synchronous. something like:

    function checkAvailability(){
            var grpname = $('#groupname').val();
            var isAvailable = false 
    
            //use ajax to run the check
            $.ajax({
                    type: 'post',
                    url: 'checkGroupName.php',
                    dataType: 'json',
                    data:  { gname: grpname },
                    async: false, 
                    success: 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 ;
    }  
    
    评论

报告相同问题?