$(document).ready( function(){ // variable to hold request var request; // bind to the submit event of our form $("#email_signup").submit(function(event){ // abort any pending request if (request) { request.abort(); } // setup some local variables var $form = $(this); // let's select and cache all the fields var $inputs = $form.find("input, select, button, textarea"); // serialize the data in the form var serializedData = $form.serialize(); // let's disable the inputs for the duration of the ajax request $inputs.prop("disabled", true); // fire off the request to /form.php var request = $.ajax({ url: "/ajax/email_signup_process.php", type: "post", data: serializedData }); // callback handler that will be called on success request.done(function (response, textStatus, jqXHR){ // log a message to the console // Set overlay to correct height $("#email_signup_error").toggle(); $("#email_signup_error").height( ($("#email_signup_div").height()-40)+'px' ); if(response == "email") { $("#email_signup_error").html('Error - Please enter a valid email address.
Click here to try again'); } else { $("#email_signup_error").html('You are now on the list for our newsletter. Thank you for your registration!'); //console.log("Hooray, it worked!"); } $("#clear_popup").click(function() { $("#email_signup_error").toggle(); }); }); // callback handler that will be called regardless // if the request failed or succeeded request.always(function () { // reenable the inputs $inputs.prop("disabled", false); }); // prevent default posting of form event.preventDefault(); }); });