tags:

views:

110

answers:

3

If I had a function like this:

function validateSomething()
{
  var valid;
  $.post("something.php", {x:y},
  function(data)
  {
     if (isSomething(data))
       valid=true; 
       //Here referring to the valid variable 
       //set outside this function, in the
       // parent function
     else
       valid=false; 
  });
  return valid/
}

Will it be possible to set valid from within the child function which is called after the Ajax request completes? If not, how can I return true/false from the parent function based on the result of the ajax request?

+2  A: 

If not, how can I return true/false from the parent function based on the result of the ajax request?

You can't; that's not how Asynchrony works.

The AJAX call may never even return!

The parent function should fire off the request for validation, and then assume it is invalid (say, by disabling the submit button) or display a "I'm thinking" feedback (spinning indicator).

Then the AJAX return handling function will clean those parts up (by enabling the button or changing the indicator to a green checkmark or red X). If the AJAX call never goes through - it stays invalid. If it does go through, that's when the clean up happens.

Tom Ritter
But this is all done as part of a form validation process. So my code goes through all the fields (when submit is clicked), validates them all, and if all the validation functions return true it then submits the form
Click Upvote
How can that be done?
Click Upvote
I answered that here: http://stackoverflow.com/questions/577231/live-username-lookup-with-ajax-jquery/577344#577344 ... and you even accepted it. async: false is the key.
Magnar
Please ask that in another question: "How can I submit a form as the result of an AJAX validation?" as it has a couple subtlies I'd like to address.
Tom Ritter
http://stackoverflow.com/questions/577632/validating-and-submitting-a-form-using-javascript-ajax
Click Upvote
+1  A: 

Valid will be updated, yes. But: The AJAX-Call is asynchronous, ie. your function will most certainly be called after the parent function returns. There is no way to wait for the asynchronous process to finish.

Ferdinand Beyer
A: 

While you can set the variable from the inner function, since the inner function is called once the post completes asynchronously it won't be set in time for the return. Depending on what you are trying to do, you may be able to handle it by having the callback function simply invoking the action that would occur depending on the value of the return. For instance, if you are clicking a link that you want to be taken if the post is successful, return false from your validateSomething function, then on successful post grab the href from the link and set location.href using it's value to simulate the effect of the link being clicked (without rerunning your validate function).

tvanfosson
Its being done as part of a loop which is called from another function (outside of validateSomething() in my example)....
Click Upvote
Then I'd either refactor your validation logic to work with asynchronous calls or change to use the ajax method and specify async=false.
tvanfosson