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.
On your php side, you should echo some
json string
, for example I'll do like this on thevalidateUser.php
:And than with jQuery :
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
json PHP
json Javascript
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