开发者

Having trouble running an if statement on a jquery ajax output

in my check.php 开发者_Go百科I have an echo "ok";

however my if statement to check if the value is ok does not work. Basically I want to execute a javascript function after check.php looks for the email in the database.

    $.ajax({                
type: "POST",
 url: "check.php",
   data: "checkit=" + $("#checkEmail").val(),
    success: function(output){
    $("#userCheck").html(output);

            if(output == "ok"){
                alert("yay");
            }
        }


    });


I would suggest returning a JSON string rather than plain text. So your check.php should echo

{status: 'ok'}

then change your ajax handler to:

$.ajax({                
    type: "POST",
    url: "check.php",
    data: "checkit=" + $("#checkEmail").val(),
    success: function(response){
        $("#userCheck").html(response.status);
        if(response.status == "ok"){
            alert("yay");
        }
    }
}, 'json');

or you could even just return a boolean value:

{success:true}


try this:

$.ajax({                
    type: "POST",
    url: "check.php",
    dataType: "text", //<---
    contentType: "application/x-www-form-urlencoded",
    data: "checkit=" + encodeURIComponent($("#checkEmail").val()),
    success: function(output){

       $("#userCheck").html(output);
       if(output == "ok")
          alert("yay");

    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜