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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器