Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simple10/6d8b7a301c36e024c651fda6ea5baf76 to your computer and use it in GitHub Desktop.
Save simple10/6d8b7a301c36e024c651fda6ea5baf76 to your computer and use it in GitHub Desktop.
Mailchimp Embed Form with Auto Populating Field Values from Query String
<!-- Begin Mailchimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">
#mc_embed_signup {
background: #fff;
clear: left;
font: 14px Helvetica, Arial, sans-serif;
width: 600px;
}
/* Add your own Mailchimp form style overrides in your site stylesheet or in this style block.
We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<form action="https://profilebp.us6.list-manage.com/subscribe/post?u=91adf7871d9348aed37379d6f&amp;id=ba70250e2e"
method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank"
novalidate>
<div id="mc_embed_signup_scroll">
<h2>Get an Invite</h2>
<div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
<div class="mc-field-group">
<label for="mce-FNAME">First Name <span class="asterisk">*</span>
</label>
<input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
</div>
<div class="mc-field-group">
<label for="mce-EMAIL">Email Address <span class="asterisk">*</span>
</label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div hidden="true">
<input type="hidden" value="" name="REFERRER" class="" id="mce-REFERRER">
</div>
<div hidden="true">
<input type="hidden" value="" name="SIGNUPURL" class=" url" id="mce-SIGNUPURL">
</div>
<div hidden="true"><input type="hidden" name="tags" value="3236921"></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_91adf7871d9348aed37379d6f_ba70250e2e" tabindex="-1" value=""></div>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe"
class="button"></div>
</div>
</form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script>
<script
type='text/javascript'>(function ($) { window.fnames = new Array(); window.ftypes = new Array(); fnames[0] = 'EMAIL'; ftypes[0] = 'email'; fnames[1] = 'REFERRER'; ftypes[1] = 'text'; fnames[2] = 'FNAME'; ftypes[2] = 'text'; fnames[3] = 'LNAME'; ftypes[3] = 'text'; fnames[4] = 'SIGNUPURL'; ftypes[4] = 'url'; fnames[5] = 'HIDDEN730'; ftypes[5] = 'text'; }(jQuery)); var $mcj = jQuery.noConflict(true);</script>
<!--End mc_embed_signup-->
<script>
/* CUSTOM CODE: POPULATE MCE FORM FIELDS */
function getParameterByName(name) {
var search = window.location.search
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)', 'i');
var results = regex.exec(search);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function populateMCEField(fieldName, value) {
var nodes = document.querySelectorAll('[id="mce-'+fieldName+'"]');
for (var i = 0; i < nodes.length; i++) {
nodes[i].value = value;
}
}
window.addEventListener('DOMContentLoaded', (event) => {
console.log('CUSTOM: populating MCE form fields');
var referrer = getParameterByName('ref');
if (referrer) {
populateMCEField('REFERRER', referrer);
}
populateMCEField('SIGNUPURL', window.location.href);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment