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.
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.
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.
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.
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.
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");
});
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With