As t.niese pointed out return true
(or false) inside success
is not doing what you probably expect it will do. (See also first comment under your question).
Probably the most straight-forward way to achieve your goal is to use whatever code you have if user (doest not) exists inside success
.
So basically move the logic/code what to do when user exists/doesn't exist inside success
callback.
For more insights you really should read up some of the links provided in the comments
Edit:
I think you miss understand the concept of an asynchronous request - given the comments in your code
$.ajax({
type:"post",
url:"actionn.php",
data:"name="+name,
success:function(data){
alert(data);
//if data does not exist means it should get out of ajax function
//ajax function has already "exited" here is the success callback
//this code is executed when the request to action.php was succesfull
//as in response code is 200/OK
}
});
//return false; //if data exists
//nope, you don't know if data exists this code will be
//executed _while_ (!) a request to action.php is made!
//there is no guarantee when this request returns and you can't write code
//here which relies on the response
//if(name=="") //data does not exist means it should come here
//{
// alert("Please enter your Name");
// return false;
//}
What you actually want to write is something like this:
$.ajax({
type:"post",
url:"actionn.php",
data:"name="+name,
success:function(data){
if(name==""){
alert("Please enter your Name");
}else{
alert("Your name is: "+data);
}
}
});
Crisp and short. All the logic depending on server's response is handleded inside success
callack. There is no code after $.ajax
which is in any way dependant on data
.
Please refrain from return
ing in $.ajax.success
- it's not what you believe it is
For further question I'd like to invite you (again) to read up about Ajax call's: https://stackoverflow.com/a/14220323/1063730 I think the paragraph about restructuring code is highly interesting for you.