Hello,
Using the Mailchimp simple signup embedded form, I try to send the submitted (email adress) using AJAX instead of the common redirection of Mailchimp. I’m nearly totally ignorant concerning AJAX. I found a solution on stackoverflow but it doesn’t work. When signing up it redirect to a page that throws a 404 error.
HTML :
<!-- Begin Mailchimp Signup Form -->
<div id="mc_embed_signup">
<form action="https://outlook.us10.list-manage.com/subscribe/post-json?u=46fbb562c588c268ad0d3f670&id=007ec4fc87&c=?" method="get" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" novalidate>
<div id="mc_embed_signup_scroll">
<div class="mc-field-group">
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Enter your email adress">
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- 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_46fbb562c588c268ad0d3f670_007ec4fc87" tabindex="-1" value=""></div>
<div class="clear"><button type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"><img src="./assets/svg/arrow-white.svg" alt=""></button></div>
</div>
</form>
<!--End mc_embed_signup-->
JS :
$(document).ready(function() {
var $form = $('mc-embedded-subscribe');
if ($form.length > 0) {
$('form input[type="submit"]').bind('click', function(event) {
if (event) event.preventDefault();
register($form);
});
}
});
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache: false,
dataType: 'json',
contentType: "application/json; charset=utf-8",
error: function(err) {
alert("Could not connect to the registration server. Please try again later.");
},
success: function(data) {
if (data.result != "success") {
// Something went wrong, do something to notify the user. maybe alert(data.msg);
} else {
// It worked, carry on...
}
}
});
}
Any idea ?
Thanks for your help.