Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused on jquery .ajax .done() function

Tags:

jquery

ajax

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.

like image 832
Vince Avatar asked Apr 18 '13 06:04

Vince


People also ask

What is done function in jQuery?

done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved. Parameters: Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.

What is the difference between done and success in AJAX?

success is the callback that is invoked when the request is successful and is part of the $. ajax call. done is actually part of the jqXHR object returned by $. ajax() , and replaces success in jQuery 1.8.

What is AJAX done function?

The done() function is given a function as parameter. The callback function passed as parameter to the done() function is executed if the AJAX request succeeds. The callback function gets three parameters: data , textStatus and jqXHR . The data parameter is the data returned by the server.

How would you fire a callback when any AJAX request on a page has completed?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.


2 Answers

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");
});
like image 98
dt192 Avatar answered Sep 24 '22 14:09

dt192


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
    }
  });
like image 42
soyuka Avatar answered Sep 23 '22 14:09

soyuka