views:

21

answers:

1

I want to include an AJAX call within a .submit() handler so that I can process form data and send email confirmations before the form is fully submitted. Therefore I'd like to make the submission wait until the AJAX call is complete, so that I can cancel the submission or do other things based on the response.

How would I go about doing this?

+2  A: 

There is no waiting in JavaScript.

The closest you can come is to stop the default action in the submit event of the form, and then call the submit method of the form element in the callback from the Ajax request.

David Dorward
Will calling `.submit()` trigger the AJAX request all over again though?
DLH
@DLH don't use submit(). Use a button's onClick to do your Ajax and call submit() in the success callback.
Fosco
@Fosco: Good idea! Why didn't I think of that?
DLH
Calling the submit method of a form won't trigger any event handlers.Don't use the onclick method of the button, it won't fire if (for example) the form is submitted by pressing enter in a text input.
David Dorward
@David: Alright, I guess I'll use submit if it won't trigger any handlers. Click wouldn't really have caused problems for me though. I only have hidden fields and radio buttons.
DLH