Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  89] [ 7]  / answers: 1 / hits: 144267  / 13 Years ago, thu, december 8, 2011, 12:00:00

Is there any way to integrate mailchimp simple (one email input) with AJAX, so there is no page refresh and no redirection to default mailchimp page.



This solution doesn't work jQuery Ajax POST not working with MailChimp



Thanks


More From » ajax

 Answers
2

You don't need an API key, all you have to do is plop the standard mailchimp generated form into your code ( customize the look as needed ) and in the forms action attribute change post?u= to post-json?u= and then at the end of the forms action append &c=? to get around any cross domain issue. Also it's important to note that when you submit the form you must use GET rather than POST.



Your form tag will look something like this by default:



<form action=http://xxxxx.us#.list-manage1.com/subscribe/post?u=xxxxx&id=xxxx method=post ... >


change it to look something like this



<form action=http://xxxxx.us#.list-manage1.com/subscribe/post-json?u=xxxxx&id=xxxx&c=? method=get ... >


Mail Chimp will return a json object containing 2 values: 'result' - this will indicate if the request was successful or not ( I've only ever seen 2 values, error and success ) and 'msg' - a message describing the result.



I submit my forms with this bit of jQuery:



$(document).ready( function () {
// I only have one form on the page but you can be more specific if need be.
var $form = $('form');

if ( $form.length > 0 ) {
$('form input[type=submit]').bind('click', function ( event ) {
if ( event ) event.preventDefault();
// validate_input() is a validation function I wrote, you'll have to substitute this with your own.
if ( validate_input($form) ) { 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...
}
}
});
}

[#88683] Tuesday, December 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
christianu

Total Points: 481
Total Questions: 124
Total Answers: 99

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
2 Years ago
;