Add a form.submit event to make sure the message occurs before the form is submitted?

I have a form. I want to add an event handler using jquery.

$("form").submit(function blah() { $.post('ping-me-please.php', params, function (response) { // only submit the form once this goes through! }); } 

How can i do this?

+4
source share
2 answers

Like this:

 $("form").submit(function (e) { var form = $(this); if (!form.data('pinged')) { e.preventDefault(); // Cancel the submit $.post('ping-me-please.php', params, function (response) { // only submit the form once this goes through! // First we set the data, then we trigger the submit again. form.data('pinged', true).submit(); }); } } 
+5
source
 var postWasSent = false; $("form").submit(function blah() { if (!postWasSent) { $.post('ping-me-please.php', params, function (response) { postWasSent=True; $("form").submit(); }); return false; } } 
0
source

All Articles