Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Grav plugin form submit (Mailchimp)

Started by Oona O'Neil 9 years ago · 5 replies · 1525 views
9 years ago

I'm trying to create a plugin to add email submitted via the newsletter form to the Mailchimp list. My question is on what event should it be initialised? I know there's this list of events available, but it's not clear to me at what point it should be initiated?

👍 1
9 years ago

If you don't want it to interact with any other actions, use onPluginsInitialized

👍 1
9 years ago

Thank for your advice. The plugin is starting to do what it supposed to. However, currently I have a different problem: I'm trying to tweak AJAX call, so it prevents page refresh and handle messages.
The code below was working before I've created calls to MailChimp API.

Currently I'm receiving this warning:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/

Any idea what's wrong and how to fix it?

JS
$(document).ready(function() {
const newsletterForm = $('.js-form-newsletter');
newsletterForm.submit(function(e) {
    // prevent form submission
    e.preventDefault();

    // submit the form via Ajax
    $.ajax({
        url: newsletterForm.attr('action') + '?submit_newsletter=1',
        type: newsletterForm.attr('method'),
        dataType: 'html',
        data: newsletterForm.serialize(),
        success: function(result) {
            $('#newsletter-form-result').html(result);
            if ($('#newsletter-form-result div.alert.notices').hasClass('green')) {
                $('.newsletter_form--hide-js').hide();
                $('.newsletter__info-text').hide();
            }
        }
    });
});
});
9 years ago

That is because you most probably want to make a cross origin ajax call (that means: url / domain of the call is a domain different from you.

Look at https://api.jquery.com/jQuery.ajax/ in the section "async".

9 years ago

Would love to see this in action. I wrote a custom plugin back in laravel and it worked swimmingly but haven't attempted in grav yet :)

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago