Confused on jquery .ajax .done() function

前端 未结 3 547
无人及你
无人及你 2021-02-08 03:29

I\'m a bit confused on how to use the ajax .done() function. I\'m working on validating a form to check if a user exists in database and though ajax would be the best approach (

3条回答
  •  离开以前
    2021-02-08 04:01

    I think it should be result == 'true' as the result is a data string

    I just checked, I am correct, the quotes make it work

    PHP:

    
    

    Javascript

    username='sambob';
    
    $(".check").blur(function(){
      $.post("validateUser.php", { username: username })
      .done(function(data) {
         if (data == 'true'){
            alert("User exists");
         }else{
            alert("User doesn't exist"); 
         }
      });
    });
    

    json PHP

    
    

    json Javascript

    $(".check").blur(function(){
       $.post("validateUser.php", { username : username },
       function(user){
          if (user.exists == true){
             alert("User exists");
          }else{
             alert("User doesn't exist");
          }
       }, "json");
    });
    

提交回复
热议问题