dongxing7318 2011-07-18 23:26
浏览 97
已采纳

Jquery Ajax PHP - 如何确定PHP代码是否成功执行?

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

4条回答 默认 最新

  • dongmai6666 2011-07-18 23:38
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图