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;
 }

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.

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

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.

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....

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.