I would like to be able to modify my sign up form code to update the groups when a user who is already subscribed fills out the form again.
I cannot get this code to update the subscriber details. Any insight would be great.
Link here: http://www.welcomeearth.tv/biet-simkin-the-art-of-being-meditation-course/#free-meditation
JS:
<script type="text/javascript">
jQuery(document).ready(function($){
ajaxMailChimpForm($("#subscribe-form"), $("#subscribe-result"));
// Turn the given MailChimp form into an ajax version of it.
// If resultElement is given, the subscribe result is set as html to
// that element.
function ajaxMailChimpForm($form, $resultElement){
// Hijack the submission. We'll submit the form manually.
$form.submit(function(e) {
e.preventDefault();
if (!isValidEmail($form)) {
var error = "A valid email address must be provided.";
$resultElement.html(error);
$resultElement.css("color", "red");
} else {
$resultElement.css("color", "red");
$resultElement.html("<!--Subscribing...-->");
submitSubscribeForm($form, $resultElement);
}
});
}
// Validate the email address in the form
function isValidEmail($form) {
// If email is empty, show error message.
// contains just one @
var email = $form.find("input[type='email']").val();
if (!email || !email.length) {
return false;
} else if (email.indexOf("@") == -1) {
return false;
}
return true;
}
// Submit the form with an ajax/jsonp request.
// Based on http://stackoverflow.com/a/15120409/215821
function submitSubscribeForm($form, $resultElement) {
$.ajax({
type: "GET",
url: $form.attr("action"),
data: $form.serialize(),
cache: false,
dataType: "jsonp",
jsonp: "c", // trigger MailChimp to return a JSONP response
contentType: "application/json; charset=utf-8",
error: function(error){
// According to jquery docs, this is never called for cross-domain JSONP requests
},
success: function(data){
var audioUrl = '/audio/hari-intro-free.mp3';
var audioPlayer = '<audio controls autoplay="autoplay"><source src="'+ audioUrl +'" type="audio/mpeg"></audio>';
if (data.result != "success") {
var message = data.msg || "Sorry. Unable to subscribe. Please try again later.";
$resultElement.css("color", "red");
if (data.msg && data.msg.indexOf("already subscribed") >= 0) {
message = "You're already subscribed.</br>Thank you.";
message += audioPlayer;
$resultElement.css("color", "red");
}
$resultElement.html(message);
} else {
$resultElement.css("color", "red");
$resultElement.html("Thank You" + audioPlayer);
}
}
});
}
});
</script>
HTML:
<form id="subscribe-form" action="http://welcomeearth.us11.list-manage.com/subscribe/post-json?u=66f9b7be393392b250e5c1a4d&id=6bfccd1b51" method="put">
<div id="form">
<div><input type="text" value="" name="FNAME" class="required" id="mce-FNAME" placeholder="First Name"></div>
<div><input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Email Address"></div>
<div class="mc-field-group input-group">
<strong>Gumroad Signups </strong>
<ul><li><input type="radio" value="131072" name="group[13893]" id="mce-group[13893]-13893-5" checked><label for="mce-group[13893]-13893-5">Biet Simikin Free Mp3 Sign Up</label></li>
</ul>
</div>
<div><input type="submit" value="FREE MEDITATION" name="subscribe"></div>
</div>
<div id="subscribe-result"></div>
</form>