0

Hi,
I have a $.ajax call in my jquery function.
I want to access the value returned from the server(php) to be accessed in jquery function. Hoe to do this??

setTimeout(function(){
	$.ajax({
          url: 'ajax-validation.php',
          data: 'email=' + $('#email').val(),
	  dataType:'text',
          type: 'post',
          success: function (resp) {
	   if(resp=="Email is available")/*This does not seems to work*/
		return true;
             else
		return false;            /*False is returned always*/
            emailInfo.text(resp);//This works. The text is shown in html page...
          }
        });},800);
    t.lastValue = t.value;
 }
3
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by lambing
0

If the result being returned from the server has leading and/or trailing blank spaces, then the if condition will always evaluate to false. Try using if( resp.indexOf("Email is available") >-1) instead.

0

Hey hielo thanks for replying..but i changed the code i'm still havin problem

setTimeout(function(){
	$.ajax({
          url: 'ajax-validation.php',
          data: 'email=' + $('#email').val(),
	  dataType:'text',
          type: 'post',
          success: function (resp) {
	if(resp=='true')
	{
	       t.lastValue = t.value;
		emailInfo.text("Email Id is available").addClass("valid");//This works
		x=true;//This assignment does not seem to work
	}
	else
	{
	       t.lastValue = t.value;
		emailInfo.text("Email Id is in use").addClass("error");//This works
		x=false;//This assignment does not seem to work
	}
       }
        });},800);
 }
}

return x;//This always returns the value to which it was initialized i.e false
0

That's because when you make an ajax call, by default you are making an asynchronous call. To clarify, given this:

alert("hello");
callSomeFunction();//let's say this function takes 5 seconds to execute
alert( "goodbye" );

normally/traditionally, you would first see "hello", then wait for 5 seconds before you see "goodbye". That is NOT what happens with asynchronous ajax calls. You would see "hello", the the function is called, but the javascript interpreter does NOT wait for it to finish. Instead it continues immediately to the next statement and executes it. So the reason you are not seeing the returned values is that by the time the ajax request completes, the other statements after you initial ajax call have executed!


So your follow up question now is "So how do I get the returned value?". That's what the callback function is for.

So if you were originally wanting to do something like:

var x = myAjaxCall();
$('#someElement').text( x );

now you will need to execute that statement within your callback function instead. Why? because the callback function executes after the asynchronous call is complete.

0

Now i understand the problem. But based on the string returned to callback i want to change the return value of function from which the ajax callback function is called. My question now is if i give return statement within the callback as you suggested would not that become the return value of callback function rather than that of the javascript function??? Is there any way to make javascript interpreter to wait for ajax call to complete(i.e make d call sync.)?
Hope my questions are clear....

Edited by hari.sarvothama: n/a

0

Is there any way to make javascript interpreter to wait for ajax call

Yes, just provide the async:false option to the ajax call:

0

Your codes are fine but I have a question though, what is the response type of your php code? is it in a json format? or just simply text like (echo "this is a text")

I think you shouldn't make your ajax to async: false because this will prevent the user from interacting in your page if the response time of your server is slow.

Edited by lambing: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.