Last active
August 29, 2015 14:02
-
-
Save mpalmr/648d499f1ae6f425f75f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Footer --> | |
<footer> | |
<!-- Newsletter Subscription --> | |
<form id="form-newsletter"> | |
<h2>Subscribe</h2> | |
<input type="email" name="email" placeholder="email" value="" required> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
<!-- Notice & Sponsor Link --> | |
<a class="become-a-sponsor" href="https://creditonline.dealertrack.ca/Web/Default.aspx?Token=629cc512-622e-4894-a25a-20c05e8b289b&Lang=en" target="_blank">Become a Sponsor</a> | |
</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, undefined) { "use strict"; | |
/* | |
* Newsletter Form Submission | |
*/ | |
var subscribeErrors = [ | |
"The information provided isn't valid.", | |
"You are already subscribed.", | |
"An error has occured. Please reload the page and try again." | |
]; | |
// All possible error messages, indexed by server response | |
$('#form-newsletter').submit(function (event) { | |
event.preventDefault(); | |
// Disable button while AJAX is executing | |
var btnSubmit = $('#form-newsletter input[type="submit"]'), | |
txtEmail = $('#form-newsletter input[type="text"]'); | |
btnSubmit.attr('disabled', 'disabled'); | |
// Submit email via POST to subscribe.php | |
var xhr = $.ajax({ | |
type: 'POST', | |
url: 'subscribe.php', | |
timeout: 7500, | |
data: { email: $('#form-newsletter input[type="text"]').val().trim() } | |
}); | |
// Enable out button activating it again | |
xhr.always(function () { | |
btnSubmit.removeAttr('disabled'); | |
$('#form-newsletter input[type="text') | |
}); | |
xhr.error(function () { | |
alert("There was a problem contacting our server. Please reload the page, and try again."); | |
}); | |
xhr.done(function (response) { | |
if (response === '0') { //Success | |
alert('You have successfully subscribed.'); | |
return true; | |
} | |
// Error | |
if (isNaN(response)) alert("There was a problem subscribing you. Please try again later."); | |
else alert(subscribeErrors[parseInt(response) - 1]); | |
return false; | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment