0

I am using ajax inside jquery to check whether the value exist in database or not. We can use normal select query(php) for this, but i have to make alert popup if the value exist. That's why i decide this way. I almost done, but problem is due to return false in jquery it does not allow to next line eventhough name does not exist in database. How can i give return true in that particular place?

$.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            
    }
});
return false; //if data exists

if(name=="") //data does not exist means it should come here
{
    alert("Please enter your Name");
    return false;
}
nuala
  • 2,681
  • 4
  • 30
  • 50
jerome
  • 31
  • 6
  • 1
    possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – t.niese Jun 21 '14 at 21:46
  • Maybe it's copied and pasted wrong but to me it seems you `return false` after submitting the ajax call. You might want to to move the code into `success` callback…? – nuala Jun 21 '14 at 21:49
  • i think you want `return true` in your success call back - maybe before the alert. erase `return false` – ewizard Jun 21 '14 at 21:50
  • @t.niese is right - the answer you want is in the link that he/she provided - look down the page after "solutions" in the answer. – ewizard Jun 21 '14 at 21:57
  • i want return true.. if data="Name already exists" means return false. But if data="" means it has to continue to next line. – jerome Jun 21 '14 at 22:03
  • Why do you have `return false;`? Is the code that you show inside of a function that you call and you want to return the information if the name exists in the database or not? If so then it is not possible, because you receive the result of the ajax call **after** you exit the function. – t.niese Jun 21 '14 at 22:07
  • @ t.niese,ok. Is there any possible way to check value exist in database or not? if exist means it should make popup alert. How to do? – jerome Jun 21 '14 at 22:14

3 Answers3

0

Just put this

return false; //if data exists

within this

success:function(data){
   alert(data);        
   //if data does not exist means it should get out of ajax function     
   return false; //if data exists - //LIKE THIS
}
DWX
  • 2,282
  • 1
  • 14
  • 15
0

You could use a if condition inside the success function like:

success:function(data) { 
    if(data) { 
         alert(data); 
    }  
    else { 
         alert("Please enter your Name"); 
         return; 
     }     
 }

See the documentation of $.ajax

  • Careful: `error` is called when HTTP communication fails, timeout, 404, 500 etc… Depending on your API you might get a good response 200/OK but an empty string which you need to check for in `success` (like you suggested) ~ it's just not exactly the same thing. :) – nuala Jun 21 '14 at 21:56
  • Yes, you are correct. I am modifying my answer. Thanks. – Ansuraj Khadanga Jun 21 '14 at 21:59
  • You can't return something from a the `success` callback that way. So the `return false;` has no effect in your code. – t.niese Jun 21 '14 at 22:00
  • It wasn't entirely wrong to suggest `error`. If the server is using more of HTTP's possibilities you could e.g. receive `401/Unauthorised` or `403/Forbidden` as resonse. Reducing the need for an if-else in `success` – nuala Jun 21 '14 at 22:05
  • @ Ansuraj Khadanga,ajax already called. i.e., data="name already exists" it is in if condition. in else condition it should not make any popup and continue to nex line(return) – jerome Jun 21 '14 at 22:23
  • I think, then you do not need the `else` part inside the success function, because anyway it will come outside the `success` function and will continue to the next statement. – Ansuraj Khadanga Jun 21 '14 at 22:30
0

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 successcallack. There is no code after $.ajax which is in any way dependant on data.

Please refrain from returning 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.

Community
  • 1
  • 1
nuala
  • 2,681
  • 4
  • 30
  • 50
  • ,it works. But how do i place if(data == ""){ return false; }else{ return true; it daoes not work?? } – jerome Jun 22 '14 at 03:55
  • I think you have a conceptual miss understanding when you try to `return` inside `success` callback. I edit my answer so hopefully it will be easier to see. – nuala Jun 23 '14 at 11:35