开发者

Jquery Ajax PHP - How to determine if the PHP code executed successfully?

So this is my idea, I have a login jquery dialog on my homepage that blocks anyone from using the webpage (behind the login form dialog). I use ajax to submit the information enter开发者_StackOverflow社区ed to a php page which then checks if the username/password combo exists in the database. If it does, I return true and then close the dialog box (allowing access to the site).

How do I use ajax to check if my php script (from the executed page) returns true or false? I was assuming the success/error options of the ajax function were checking this, but the below code doesnt seem to work.

    $('.login').dialog({
    closeOnEscape: false,
    title: false,
    modal: true,
    width: 'auto',
    show: {effect: "fade", duration: 1500},
    title: 'Login',
    buttons: {
        "Login": function() { 
            $.ajax({
                async: false,
                type: 'POST',
                url: 'sql/login.php',
                success: function(){
                    alert( "Success!");
                },
                error: function(){
                    alert( "Failed!" );
                },
                dataType: "html"
            });
        }
    }
});
$(".login").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 


What I did was to put an echo at the end of my php with the variabel that is put in true or false, the I did this function inside the success option it is like this:

success: function(response){
                    if(response){
                            alert( "Success!");
                            }else{
                            alert( "Failed!" );
                                 }

                    }

This worked for me, and you can put this in the php

if($myvar){
$anothervar=1;
}else{
$anothervar=0;
}
echo $anothervar;

so you can do this:

success: function(response){
                    if(response==1){
                            alert( "Success!");
                            }else{
                            alert( "Failed!" );
                                 }

                    }

this is the way that I do it and it works fine Hope it is useful


No. The success and error options relate to the success or failure of the XHR request. You need your php script to output something and check that. For example if your php script outputs "ok" (i.e. echo("ok");) if the verification is successful, you would use an ajax request such as this one

$.ajax({
  url: 'sql/login.php',
  success: function(data) {
    if(data=="ok") { 
       alert('Login successfull.');
    } else {
       alert('Login failed.');
    }
  }
});


In your login.php:

   if($_POST['data']){

        $curData = $_GET['country'];
        $query = "SELECT * FROM yourTable 
                    WHERE tblData = '$curData'";
        $result = mysql_query($query) or die;

        if($result>0)
               echo "<p class='checkExistance'>Data Exists</p>";
            else
               echo "<p class='checkExistance'>Data doesnt Exist</p>";
    }

In your javascript:

function isSuccess(){
    if($('.checkExistance').text() === "Data Exists"){
             success($('.checkExistance').text()); //alert success!
    }else{
           success($('.checkExistance').text());
    }
}

Did not really check if that works, but you pretty much get the idea. On AJAX call, do this:

$.ajax({
   //
   ..
   success:isSuccess

});

or 

success:function(){
    if($('.checkExistance').text() === "Data Exists"){
                 success($('.checkExistance').text()); //alert success!
        }else{
               success($('.checkExistance').text());
        }
}

Make sure you implement alert() within the success() function..Hope that helps.


According to your information I assumed that your php page will do a validation and you make them return true or false within the php script

There are 2 problems in your code

1.your php page will return true or false within itself,in the other hand they won't send the result to ajax result unless you send them out like Tomm's said above

2.success() and error() of $.ajax are determined by the success of request NOT BY RESULT which mean that even if your php echo "false" it will trigger success() event because THE REQUEST IS SUCCESSFULLY SENT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜