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 に答える