Skip to content

Instantly share code, notes, and snippets.

@bheyde
Created June 10, 2015 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bheyde/a808800fbb14ddebaa45 to your computer and use it in GitHub Desktop.
Save bheyde/a808800fbb14ddebaa45 to your computer and use it in GitHub Desktop.
Mailchimp JavaScript
//Success Message. Message that fades in once the user has successfully been subscribed.
var success_message = "Successfully Subscribed";
//Success Message. Message that fades in once the user has successfully been subscribed.
var already_subscribed_message = "Your are already subscribed to our mailing list.";
//Unsubscribe Message. Message that shows up when a user has removed their email from your mailing list.
var unsubscribe_message = "You have been removed from our email list";
//This is the color that will be used for both the subscribe and unsubscribe error messages.
var message_color = "#2398C9";
//preload loading gif
if(loading_path){
loading = new Image();
loading.src = loading_path;
}
function addSubscriber(api_key, id, email,firstname,lastname,response_message_id){
if( window.location.href.indexOf("https") != -1 ){
url = "https://" + window.location.hostname + module_path + "?action=subscribe&api_key=" + encodeURIComponent(api_key) + "&id=" + id + "&email_address=" + email + "&merge_vars[FNAME]=" + firstname + "&merge_vars[LNAME]=" + lastname + "&output=json";
}else{
url = "http://" + window.location.hostname + module_path + "?action=subscribe&api_key=" + encodeURIComponent(api_key) + "&id=" + id + "&email_address=" + email + "&merge_vars[FNAME]=" + firstname + "&merge_vars[LNAME]=" + lastname + "&output=json";
}
if(response_message_id == 'checkbox_message'){
$('#' + response_message_id ).css('display','block');
}
if(loading_path){
$('#' + response_message_id ).html('<img src=' + loading_path + ' border="0" />');
}
$.get(url, function(data){
data = jQuery.trim(data);
if(data == "true"){
$('#' + response_message_id ).css('color',message_color);
$('#' + response_message_id ).text(success_message).fadeIn(1000) ;
}else{
data = data.replace(/(\r\n|\n|\r)/gm,"");
var myObject = jQuery.parseJSON( data );
$('#message').css('color',message_color);
if(myObject.code == 214){
$('#' + response_message_id ).html(already_subscribed_message).fadeIn(1000);
}else{
$('#' + response_message_id ).html(myObject.error).fadeIn(1000);
}
if(document.getElementById('newsletter_checkbox') != null){
$('input[name=newsletter]').attr('checked', false);
}
}
}); //end .get
if(response_message_id == 'checkbox_message'){
setTimeout(function() { $('#' + response_message_id ).css('display','none').fadeOut(slow); }, 7000);
}
} //end add Subscriber
function removeSubscriber(api_key, id, email,delete_member,send_goodbye,send_notify,response_message_id){
if( window.location.href.indexOf("https") != -1 ){
url = "https://" + window.location.hostname + module_path + "?action=unsubscribe&api_key=" + encodeURIComponent(api_key) + "&id=" + id + "&email_address=" + email + "&delete_member=" + delete_member + "&send_goodbye=" + send_goodbye + "&send_notify=" + send_notify +"&output=json";
}else{
url = "http://" + window.location.hostname + module_path + "?action=unsubscribe&api_key=" + encodeURIComponent(api_key) + "&id=" + id + "&email_address=" + email + "&delete_member=" + delete_member + "&send_goodbye=" + send_goodbye + "&send_notify=" + send_notify +"&output=json";
}
if(response_message_id == 'checkbox_message'){
$('#' + response_message_id ).css('display','block');
}
if(loading_path){
$( '#' + response_message_id ).html('<img src=' + loading_path + ' border="0" />');
}
$.get(url, function(data){
data = $.trim(data);
if(data == "true"){
$('#' + response_message_id ).css('color',message_color);
$('#' + response_message_id ).text(unsubscribe_message).fadeIn(1000);
}else{
data = data.replace(/(\r\n|\n|\r)/gm,"");
var myObject = jQuery.parseJSON( data );
$('#' + response_message_id ).css('color',message_color);
$('#' + response_message_id ).html(myObject.error).fadeIn(1000);
}
});
if(response_message_id == 'checkbox_message'){
setTimeout(function() { $('#' + response_message_id ).css('display','none').fadeOut(slow); }, 7000);
}
}
function disableCheckbox(){
$('#newsletter_checkbox').attr('disabled', 'disabled');
}
function enableCheckbox(){
$('#newsletter_checkbox').removeAttr('disabled');
}
function addEmail(){
var email = $('#mailchimp_email').val();
if( api_key == '' || list_id == ''){
alert("Please enter your MailChimp API Key and select a list in the Miva Merchant admin under utilities.");
}else{
addSubscriber(api_key, list_id, email, '', '','message');
}
return false;
}
function removeEmail(){
var email = $('#mailchimp_email').val();
if( api_key == '' || list_id == ''){
alert("Please enter your MailChimp API Key and select a list in the Miva Merchant admin under utilities.");
}else{
removeSubscriber(api_key, list_id, email, 'false', 'false', 'false','message');
}
return false;
}
function mailchimp_checkbox(){
//set variables
var isChecked = $('input[name=newsletter]:checked').val();
var shippingEmail = $('#ShipEmail').val();
var shippingFirstName = $('#ShipFirstName').val();
var shippingLastName = $('#ShipLastName').val();
if (shippingEmail == ''){
alert("Please enter your email address above and try again.");
$('input[name=newsletter]').attr('checked', false);
}else if(shippingFirstName == ''){
alert("Please enter your first name above and try again.");
$('input[name=newsletter]').attr('checked', false);
}else if(shippingLastName == ''){
alert("Please enter your last name above and try again.");
$('input[name=newsletter]').attr('checked', false);
}else{
if( api_key == '' || list_id == ''){
alert("Please enter your MailChimp API Key and select a list in the Miva Merchant admin under utilities.");
}else{
if(isChecked){
disableCheckbox();
addSubscriber(api_key, list_id, shippingEmail, shippingFirstName, shippingLastName, 'checkbox_message');
setTimeout("enableCheckbox()",2000);
}else{
disableCheckbox();
removeSubscriber(api_key, list_id, shippingEmail, 'false', 'false', 'false', 'checkbox_message');
setTimeout("enableCheckbox()",2000);
}
} //end api key check
} //end input valudation
} // end mailchimp checkbox
function mailchimp_textbox(){
$('#mailchimp_email').focus(function(){
$('#mailchimp_email').css('color', '#666');
});
$('#mailchimp_email').focus(function(){this.value=''}).blur(function(){if(this.value==''){this.value='Enter Email Address'}});
$('#newsletter_form').unbind("submit").submit(function(){
var email = $('#mailchimp_email').val();
if(email != 0){
if(isValidEmailAddress(email)){
addEmail();
return false;
}else{
$('#mailchimp_email').val('Invalid Email Address');
$('#mailchimp_email').css('color', '#EF2C2C');
return false;
}
}
});
$('#remove-link').unbind("click").click(function(){
var email = $('#mailchimp_email').val();
if(email != 0){
if(isValidEmailAddress(email)){
removeEmail();
return false;
}else{
$('#mailchimp_email').val('Invalid Email Address');
$('#mailchimp_email').css('color', '#EF2C2C');
return false;
}
}
});
} // end mailchimp_textbox
function isValidEmailAddress(emailAddress){
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
$(document).ready(function () {
var checkbox_present = document.getElementById("newsletter_checkbox");
var textbox_present = document.getElementById("mailchimp_email");
if (checkbox_present != null){
$('#newsletter_checkbox').unbind("click").click(function () {
mailchimp_checkbox();
});
}
if (textbox_present != null){
mailchimp_textbox();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment