1

I'm using the MailChimp Subscribe for Craft plugin to embed several subscription forms on a website I'm building.

The following code appears via my master _layout.html file:

<form action="" method="POST">

    <input type="hidden" name="action" value="mailchimpSubscribe/list/Subscribe">
    <input type="hidden" name="redirect" value="successfully-subscribed">

    {# MailChimp List ID #}
    <input type="hidden" name="lid" value="XXXXXXXXXX">

    {% if mailchimpSubscribe is defined %}
        {% if (not mailchimpSubscribe.success) and (mailchimpSubscribe.errorCode!='1000') %}
            <p>An error occured. Please try again.</p>
        {% endif %}
    {% endif %}

    <div class="field-line">
        <label>Name</label>
        <input type="text" placeholder="Name" name="mcvars[NAME]" value="{% if (mailchimpSubscribe is defined) and (not mailchimpSubscribe.success) %}{{ mailchimpSubscribe.values.vars.NAME }}{% endif %}" required>
    </div>

    <div class="field-line">
        <label{% if (mailchimpSubscribe is defined) and (mailchimpSubscribe.errorCode=='1000') %} class="error"{% endif %}>Email:</label>
        <input type="email" placeholder="Email Address" name="email" value="{% if (mailchimpSubscribe is defined) and (not mailchimpSubscribe.success) %}{{ mailchimpSubscribe.values.email }}{% endif %}" required>
    </div>

    <input type="submit" name="" value="Go!">

</form>

When I hit go, I get the following Bad Request error:

Bad Request

I've temporarily fixed the error via general.php by disabling CSRF Protection (not recommended).

How do I implement CSRF Protection on this form? I've followed this article, but replacing the name and value fields will surely disable the plugin's functionality?

Should I also implement the method shown here for other forms like Am Forms?

Update: There's a pull request on the plugin git page regarding this.

3

Enable CSRF protection in your config again and add {{ getCsrfInput() }} right after the <form> opening tag on all forms on your site, regardless if they are amforms or mailchimp subscribe. I can verify that it won't interfere with the mailchimp plugin as I am currently (and just recently) installed it on a project I'm working on and it works fine.

| improve this answer | |
  • Excellent, thank you. I can confirm it's working perfectly. – Sam Sep 27 '17 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.