Mailchimp Submitter
Easily integrate your HTML forms with Mailchimp JSONP Ajax.
Installing
yarn add mailchimp-submitter
npm install mailchimp-submitter
Basic Usage
-
Create your HTML form
<form data-mc="true" id="my-form">
<input type="hidden" name="project" value="MY-PROJECT-NAME" />
<input type="hidden" name="datacenter" value="us7" />
<input type="hidden" name="u" value="XXXXXXXXXXXXXXXXXXXXXXXXX" />
<input type="hidden" name="id" value="XXXXXXXXXX" />
<input type="email" name="EMAIL" />
<input type="text" name="FNAME" />
<input type="text" name="LNAME" />
<button>Subscribe</button>
</form>
-
Import functions in your JavaScript and initialize Mailchimp Submitter
import MailchimpSubmitter from 'mailchimp-submitter'
MailchimpSubmitter()
Advanced Usage
Using callbacks as arguments
MailchimpSubmitter({
beforeSubmit: _formEl => {
const isValid = validateForm()
return isValid
},
callback: (_formEl, resp) => {
if (resp.result === 'success') {
alert('Successfully subscribed')
} else {
alert('Error: ' + resp.msg)
}
},
})
Using callbacks as event listeners
const myForm = document.getElementById('my-form')
myForm.addEventListener('mcBeforeSubmit', () => {
console.log('Submitting form...')
})
myForm.addEventListener('mcCallback', ({ detail: resp }) => {
if (resp.result === 'success') {
alert('Successfully subscribed')
} else {
alert('Error: ' + resp.msg)
}
})