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

所以这是我的想法,我的主页上有一个登录jquery对话框阻止任何人使用该网页(后面 登录表单对话框)。 我使用ajax将输入的信息提交到php页面,然后检查数据库中是否存在用户名/密码组合。 如果是,我返回true然后关闭对话框(允许访问该站点)。</ p>

如何使用ajax检查我的php脚本(来自执行的页面)是否返回 是真还是假?
我假设ajax函数的成功/错误选项正在检查这个,但下面的代码似乎不起作用。</ p>

  $('。login'  ).dialog({
closeOnEscape:false,
title:false,
modal:true,
width:'auto',
show:{effect:“fade”,duration:1500},
title :'登录',
按钮:{
“登录”:function(){
$ .ajax({
async:false,
type:'POST',
url:'sql / login。 php',
成功:function(){
alert(“成功!”);
},
错误:function(){
alert(“失败!”);
},
dataType :“html”
});
}
}
});
$(“。login”)。dialog()。parents(“。ui-dialog”)。find(“。ui- 对话框的标题栏“)除去();

</ code> </ pre>
</ div>

展开原文

原文

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(); 

douzhaiya3968
douzhaiya3968 只是想把它扔出去。如果用户未经授权,您是否仅通过客户端方法(javascript)限制对内容的访问?如果是这样,您将需要重新考虑,因为任何使用Firebug的人都可以绕过您的安全。
9 年多之前 回复

4个回答



我所做的是在我的php结尾处放置一个回声,其中包含真或假的变量,我做了 成功选项中的这个函数是这样的:</ p>

  success:function(response){
if(response){
alert(“Success!”); \ n} else {
alert(“Failed!”);
}

}
</ code> </ pre>

这对我有用,你可以把它放进去 php </ p>

  if($ myvar){
$ anothervar = 1;
} else {
$ anothervar = 0;
}
echo $ anothervar; \ n </ code> </ pre>

所以你可以这样做:</ p>

  success:function(response){
if(response == 1){
alert(“成功!”);
}其他{
alert(“失败!”);
}

}
</ code> </ pre>

此 是我这样做的方式,它工作正常
希望它是有用的</ p>
</ div>

展开原文

原文

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

duanliaouu965826
duanliaouu965826 搞定了! 谢谢!
9 年多之前 回复
drhzn3911
drhzn3911 那么您的查询可能存在问题
9 年多之前 回复
douhuang75397
douhuang75397 我试过了,并没有奏效。 总是返回'错误!'
9 年多之前 回复



不。 成功和错误选项与XHR请求的成功或失败有关。 你需要你的PHP脚本输出一些东西并检查。 例如,如果您的php脚本输出“ok”(即 echo(“ok”); </ code>),如果验证成功,您将使用ajax请求,例如此</ p>
\ n

  $ .ajax({
url:'sql / login.php',
success:function(data){
if(data ==“ok”){
alert(' 登录成功。');
} else {
alert('登入失败。');
}
}
});
</ code> </ pre>
</ div>

展开原文

原文

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.');
    }
  }
});



在你的login.php中:</ p>

  if($ _ POST ['data'  ]){

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

if if($ result&gt; 0)
echo“&lt; p class ='checkExistance'&gt; Data Exists&lt; / p&gt;”;
else
echo“&lt; p class ='checkExistance' &gt;数据不存在&lt; / p&gt;“;
}
</ code> </ pre>

在您的javascript:</ p>

 函数中 isSuccess(){
if($('。checkExistance')。text()===“Data Exists”){
success($('。checkExistance')。text()); //警告成功!
}其他{
成功($('。checkExistance')。text());
}
}
</ code> </ pre>

没有真正检查是否有效,但你几乎明白了。 在AJAX调用上,执行以下操作:</ p>

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

});

or

成功:function(){
if($('。checkExistance')。text()===“Data Exists”){
success($('。checkExistance')。text()); //警告成功!
}其他{
成功($('。checkExistance')。text());
}
}
</ code> </ pre>

确保在success()函数中实现alert()。希望有所帮助。</ p>
</ div>

展开原文

原文

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.



根据您的信息,我假设您的php页面将进行验证,并使它们在php脚本中返回true或false </ p>

你的代码中有2个问题</ p>

1.你的php页面本身会返回true或false,另一方面他们不会发送 导致ajax结果,除非你像Tomm上面说的那样发送它们</ p>

2.success()和$ .ajax的错误()由请求NOT BY RESULT的成功决定,这意味着 即使你的php回显“false”,它也会触发success()事件,因为请求是成功的。</ p>
</ div>

展开原文

原文

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.

立即提问
相关内容推荐