0

我正在尝试使用 ajax 和 jquery 发送表单以在同一页面上获取结果。我有这个代码,我从我的表单操作中链接:

function check()
{

    var html = $.ajax({
        type: "POST",
        url: "reg.php",
        data: $("#reg").serialize(),
        async: false
        }).responseText;
    if(html == "success") { 


        //Indicate a Successful Captcha
        $("#captcha-status").html("<p class=\"green bold\">Success! Thanks you may now proceed.</p>");
    } else {
        $("#captcha-status").html("<p class=\"red bold\">The security code you entered did not match. Please try again.</p>");

    }
}    

然后我得到了我的 php 文件:

$query = "SELECT * FROM teemo1 WHERE nick='$nick' and server='$server'";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) )
{
    echo 'success';
}
else
{
$queryy =  

     echo 'fail';
}

 }

我需要在我的页面中显示表单“数据已成功保存”,如果失败“数据不正确”,但我不知道如何将这些回声从 php 文件获取到我的 ajax 脚本。

谢谢

4

2 回答 2

1

尝试这个 :

function check(){
    $.ajax({
    type: "POST",
    url: "reg.php",
    data: $("#reg").serialize(),
    async: false,
    success:function(html){
        if(html == "success") { 
            $("#captcha-status").html("<p class=\"green bold\">Success! Thanks you may now proceed.</p>");
        } else {
            $("#captcha-status").html("<p class=\"red bold\">The security code you entered did not match. Please try again.</p>");
            }
        }
    });
}   
于 2012-12-26T16:13:37.823 回答
0

您需要使用successfunction 并使用 success function 的传递值parameter,这就是responseText您的情况。

$.ajax({
    type: "POST",
    url: "reg.php",
    data: $("#reg").serialize(),
    async: false
}).success(function(responseText){        
     if(responseText == "success") {       
      $("#captcha-status").html("<p class=\"green bold\">Success! Thanks you may now proceed.</p>");
     } else {
         $("#captcha-status").html("<p class=\"red bold\">The security code you entered did not match. Please try again.</p>");

     }
});
于 2012-12-26T16:14:45.177 回答