1. Web Design
  2. Email
  3. Email Marketing

How to Code a Custom Mailchimp Popup Form

Scroll to top
Read Time: 6 min

In this tutorial, we’ll learn how to customize Mailchimp’s embedded form and include it within a custom JavaScript modal popup. 

Here’s what our Mailchimp popup modal will look like

here's our popup formhere's our popup formhere's our popup form

There are all kinds of marketing platforms out there like MailchimpSendinblueContactPigeon, and Constant Contact. These can help you grow your business and generate more leads by providing you with the tools for sending marketing messages, segmenting audiences, creating customer journeys, and so on. All come with different pricing plans—some even offer a free service. Plus, depending on your platform (eCommerce, CMS, CRM), they might deliver built-in integrations. For example, WordPress Contact Form 7 (CF7) plugin provides an integration module with Constant Contact.

One popular example of a marketing campaign that you might have seen in different eCommerce stores is to receive a one-off discount upon a newsletter subscription. The journey might look as follows:

  • First, you subscribe to the newsletter.
  • Then, an automated email is sent immediately to your address with a one-time discount promo code which you can use for a limited period.

But things aren’t limited only to stores. Another example might be a college website form where a candidate can express their interest in a program. The journey here might look as follows:

  • First, you fill out the form by expressing your interest in a program.
  • Then, an automated email is sent immediately to your address with an attached file that describes your chosen program in more detail.
  • At the same time, your email address is added to a list with specific tags, for example, "BSc - Informatics - 2023-2024". This will help the marketing team behind the college to categorize audiences and run promotions from time to time.

About Our Project

With all the above in mind, our goal for today is to embed a Mailchimp sign-up form for receiving a 10% discount code within a custom modal popup.

Here’s an introductory video that demonstrates the form layout:

The form will be fully functional, and upon form submission, the subscriber’s info will be added to the selected list/audience. From there, you can continue the customer journey as you wish.

Here’s Our Plan

  • We won’t build the modal from scratch but instead reuse the one that we built in a previous tutorial.  
  • We’ll generate the form by using Mailchimp’s form builder and customize it.

4 Things to Keep in Mind

  • Even though I’ll work with the Mailchimp platform here, I’m pretty confident that at least some of the other platforms will provide form builders with generated HTML that you can use in the same way.
  • I’m using a free plan here. If you have a more premium plan, you can certainly have more options like choosing the audience, etc.
  • The project will be hosted on GitHub. I won’t provide code snippets inside the tutorial. Feel free to download the project and check it. Again, most of the code will rely on the modal that we’ve already built.
  • The form submission won’t work as I’ve removed the action URL. To test the demo, you have to create a new form using the form builder from your own account and replace the action attribute value. Even better, for more safety—in case there will be any functional differences in the future, clone the repo, and replace any Mailchimp URLs with the ones from your generated code. Be sure to examine any other things (elements, classes, etc.) that might have changed since then and not working as expected.

Code for the Modal

As discussed above, in a previous tutorial, we created a flexible JavaScript popup modal from scratch with a customizable animation effect (fade, slide, bounce, etc.) of its dialog.  

Here’s the demo:

For this example, our modal will launch via the slideInOutLeft animation (click on the first button above to see it in action). We’ll keep all modal’s core functionality untouched and only make some little stylistic modifications like increasing its height and rounded corners. 

We’ll also provide two ways for revealing the modal:

  • First, upon button click, as it happens already.
  • Alternatively, the modal can autoload when the page loads with a small delay. Then, upon the user closes it, it won’t open again unless they open a new browser session. For this case, we’ll add a small amount of JavaScript code and activate the session storage. Of course, if you want a more permanent solution, you can replace session storage with local storage.
Both local storage and session storage are used to store data in the browser. Local storage doesn’t expire unless you clear the data on your own. On the contrary, session storage is cleared when the page session ends. Learn more about this topic in the MDN docs.

Create the Mailchimp Form

The following steps require that you have a Mailchimp account. After signing in to the Mailchimp dashboard, we’ll perform three actions:

First, we’ll generate the form by selecting the Embedded forms option.

How to get the HTML code from a Mailchimp formHow to get the HTML code from a Mailchimp formHow to get the HTML code from a Mailchimp form

Then, in the Form Builder, apart from the predefined Email input, we’ll add two more required ones.

How to get the HTML code from a Mailchimp formHow to get the HTML code from a Mailchimp formHow to get the HTML code from a Mailchimp form

The order doesn’t matter, as we can also manually reposition them later in the HTML.

Finally, by clicking on the Complete button on the top right, we’ll get the generated code.

The generated codeThe generated codeThe generated code
Mailchimp gives also the option to export the generated code without styles, but we won’t go with this approach.

Embed the Code

We’ll take the generated code from Mailchimp and paste it into our modal:

1
<div class="modal-dialog">
2
    <!--paste contents here-->
3
</div>

Edit the Generated Code (a Bit)

Our form currently looks like this:

The Mailchimp form before our changesThe Mailchimp form before our changesThe Mailchimp form before our changes
The Mailchimp form before our changes

We’re going to make some aesthetic improvements though. Here are the main modifications we’ll make to this code:

  • To make things more readable, we’ll put the related Mailchimp CSS and JS files right before the closing head and body tags of our page, respectively.
  • To create the two-column layout, we’ll wrap the two text inputs inside an element with the grid class.
  • We’ll remove the labels from all inputs and give them an extra placeholder attribute.
  • We’ll change some texts, and remove some extra elements/classes that we won’t need like the .indicates-required and .helper_text ones.
  • We’ll add some custom styles.

Our form will now look like this:

The Mailchimp form after our changesThe Mailchimp form after our changesThe Mailchimp form after our changes
The Mailchimp form after our changes

Form Validation

If someone tries to submit the form without filling in all the fields, styled messages will appear.

Unsuccessful form submissionUnsuccessful form submissionUnsuccessful form submission

Success Confirmation

As soon as someone fills out the form successfully, they’ll be added to the target audience—see both the front and back screens below.

The message that appears after the form is sentThe message that appears after the form is sentThe message that appears after the form is sent
The subscribed usersThe subscribed usersThe subscribed users

Double Opt-in for New Recipients

As an extra verification step, there’s also the option to enable the double opt-in from the form settings.

How to enable double opt-inHow to enable double opt-inHow to enable double opt-in

In such a case, the user needs first to confirm their subscription—then they’ll be added to the audience.

The message that appears after the form is sent when double opt-in is enabledThe message that appears after the form is sent when double opt-in is enabledThe message that appears after the form is sent when double opt-in is enabled
The mail confirmation when the double opt-in option is enabledThe mail confirmation when the double opt-in option is enabledThe mail confirmation when the double opt-in option is enabled

Conclusion

That’s it! Let’s recap what we built today:

  • First, we covered how to set up and customize an embedded Mailchimp sign-up form, and then
  • how to include it inside a custom JavaScript modal popup.

As mentioned in the beginning, this demo won’t work unless you change the value of the action attribute.

Of course, Mailchimp provides other ways for embedding a subscription form in your web pages. Similarly, there are plugins for different platforms out there ready to handle all this stuff for you.

As always, thanks a lot for reading!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.