Last active
October 25, 2018 11:11
-
-
Save celsowhite/c0c519badc62b4c53228c4b92a090f77 to your computer and use it in GitHub Desktop.
Snippet for Mailchimp ajax form submissions.
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
<!-- Email Signup HTML --> | |
<div class="mc_embed_signup"> | |
<form action="//trago.us11.list-manage.com/subscribe/post-json?u=6a6ec6ff09e1e698609b74747&id=b6a9a7c361&c=?" method="post" name="mc-embedded-subscribe-form" class="validate mc-embedded-subscribe-form newsletter__form" novalidate> | |
<input type="submit" value="Submit" name="submit" id="mc-embedded-subscribe" class="button"><input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL"> | |
<!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--> | |
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_bfaeb9f9cfe94bf88ebe82105_58d7ecad1c" tabindex="-1" value=""></div> | |
</form> | |
<div class="mce-responses" class="clear"> | |
<div class="response mce-error-response" style="display:none"></div> | |
<div class="response mce-success-response" style="display:none"></div> | |
</div> | |
</div> | |
/*================================= | |
CSS | |
=================================*/ | |
.mc_embed_signup input { | |
border: 0; | |
border-radius: 0; | |
padding: 5px 10px; | |
} | |
.mc_embed_signup input[type="submit"] { | |
background: $gold; | |
color: $white; | |
text-transform: uppercase; | |
} | |
.mce-responses .response { | |
font-size: 10px; | |
color: $white; | |
} | |
/*================================= | |
MAILCHIMP AJAX | |
=================================*/ | |
// Set up form variables | |
var mailchimpForm = $('.mc-embedded-subscribe-form'); | |
// On submit of the form send an ajax request to mailchimp for data. | |
mailchimpForm.submit(function(e){ | |
// Set variables for this specific form | |
var that = $(this); | |
var mailchimpSubmit = $(this).find('input[type=submit]'); | |
var errorResponse = $(this).closest('.mc_embed_signup').find('.mce-error-response'); | |
var successResponse = $(this).closest('.mc_embed_signup').find('.mce-success-response'); | |
// Make sure the form doesn't link anywhere on submit. | |
e.preventDefault(); | |
// JQuery AJAX request http://api.jquery.com/jquery.ajax/ | |
$.ajax({ | |
method: 'GET', | |
url: that.attr('action'), | |
data: that.serialize(), | |
dataType: 'jsonp', | |
success: function(data) { | |
// If there was an error then show the error message. | |
if (data.result === 'error') { | |
// If the error has an error code at front then alter the message to not include the error code and hyphen. | |
if(data.msg.indexOf('0') === 0) { | |
data.msg = data.msg.slice(3); | |
} | |
errorResponse.html(data.msg).fadeIn(300).delay(3000).fadeOut(300); | |
} | |
// If success then show message | |
else { | |
successResponse.html('Success! Please check your email for a confirmation message.').fadeIn(300).delay(3000).fadeOut(300); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment