12

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 (still learning it). I have a php file that return true if user exists and false if user doesn't exist. How would I pass the boolean into the parameter for the done function?

$(".check").blur(function(){
  var username = $(".check").val();
  $.ajax({
    url: "validateUser.php",
    type: "post",
    data: "username= " + username
  }).done(function(result){
    if (result == true){
      // User exists
    }else{
      // User doesn't exist
    }
  });
});

I hope that made sense. I did my best to explain it.

Vince
  • 2,596
  • 11
  • 43
  • 76
  • I am slightly confused about what you're asking, does the above not work? – dt192 Apr 18 '13 at 06:36
  • @DanielTillin I saw an example on the jquery website similar to mine, but I edited it to suit me best, but I'm not sure where they get the 'result' parameter from. – Vince Apr 18 '13 at 06:45
  • 1
    the result is the data that is returned from the post, but the returned data is a string, so therefor you will match 'true' or 'false' quoted if that's what your php file outputs – dt192 Apr 18 '13 at 06:53
  • @DanielTillin It's somewhat working. For some reason it's only returning that the user doesn't exist, which is the same problem I was having with the jquery validation engine. It would only return that the user didn't exist (even when they do exist) so I decided to write my own function...which isn't working too well. I'm basically back to square one. – Vince Apr 18 '13 at 07:09
  • I've put one together and mine is working fine, ill post it below – dt192 Apr 18 '13 at 07:19
  • ok try that and let me know if it works for you – dt192 Apr 18 '13 at 07:24
  • ok i added the json versions of the same thing, that's working for me – dt192 Apr 18 '13 at 08:08

3 Answers3

8

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:

<?php

if($_POST['username']=='sambob'){echo 'true';}else{echo 'false';}

?>

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

<?php

if($_POST['username']=='sambob'){echo'{"exists":true}';}
                            else{echo'{"exists":false}';}
?>

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");
});
dt192
  • 1,003
  • 7
  • 12
  • Okay, so I compared what you have to what I have, and it turns out the error was in my PHP code. When I returned true or false, I didn't have them surrounding in quotes, and I didn't echo them. I used return instead. – Vince Apr 18 '13 at 07:27
  • 1
    no probs, I've been there many times myself it's always the simple things that are hard to track down, good luck ;-) – dt192 Apr 18 '13 at 07:32
  • Hmm I don't agree to the fact that true should be a String it should be a boolean. I never had trouble with them before, just use `JSON` and everything will be working as expected. – soyuka Apr 18 '13 at 07:39
  • data returned through the ajax function always comes back as a string unless otherwise specified, so it would never be a proper true, not with default settings – dt192 Apr 18 '13 at 07:46
5

On your php side, you should echo some json string, for example I'll do like this on the validateUser.php :

//Check your database etc.
$user_exists = array('error'=>false,'user'=>true);
echo json_encode($user_exists);

And than with jQuery :

$.ajax({
    url: "validateUser.php",
    type: "post",
    data: "username= " + username,
    dataType: "json",
  }).done(function(result){
    if (result.error == false){
        //No errors, check user
        if(result.user == true)
            alert("Exists"); //now do some stuff
        else
            alert("User don't exists");
    }else{
      // There is an error
    }
  });
soyuka
  • 8,839
  • 3
  • 39
  • 54
0

Success: It will return the XHR success status, eg: 200
Done: Once XHR get success then it will complete the and return with a return data

Try below code

 $.ajax({
type: "POST",
url: Url,
data: dataParameter,
async: true,
success: function(results, textStatus) {
    debugger;
    console.log("success : " + results);
},
error: function(xhr, status, error)
{
    console.log("error : " + xhr.responseText);                   
}
}).done(function(results) {
    debugger;          
    console.log("done : " + results);           
});
Dorjee Karma
  • 339
  • 2
  • 5