Confused on jquery .ajax .done() function -
i'm bit confused on how use ajax .done() function. i'm working on validating form check if user exists in database , though ajax best approach (still learning it). have php file return true if user exists , false if user doesn't exist. how pass boolean parameter 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 made sense. did best explain it.
i think should result == 'true' result data string
i checked, correct, quotes make 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"); });
Comments
Post a Comment