I’m trying to send data from a form to an external script prior to submitting the form, yet I cannot seem to get the data to reach the external script unless I return false; on the form itself.
$(document).ready(function () { // Handle Form-Submission $('#mainForm').submit(function () { // Reset Error Array errors = new Array(); /* Validation Code Removed - Not Relevant */ // Check if errors exist if (errors.length > 0) { return false; } else { $('div.errors').html(''); $.post('post.php',{ 'First Name': name_first.val(), 'Last Name': name_last.val(), 'Home State': home_state.val(), 'Primary Email': email_primary.val() }); } return false; /* Remove this line, and the $.post won't work. */ }); });
I ran into the exact same problem today. Like Marc says, it’s because the ajax call is asynchronous. The simplest fix is to make it synchronous.
Use .ajaxSetup() before any ajax calls like such: