I am using the hosted version of Locomotive CMS.
Locomotive CMS provides a means by which to collect form submissions and a means by which to send an email using the Actions API.
I want to collect form submissions and then send an email to the person who submitted the form.
I have 3 caveats:
- I wish to collect the form data in a content type (public submissions enabled)
- I wish to use Google reCAPTCHA v3
- I wish to submit the form using AJAX
All of the aforementioned items are available out of the box in the hosted version of Locomotive CMS. However, I can't figure out how to submit a content type form with Google reCAPTCHA via AJAX and send an email at the same time. The Send Email functionality requires an action on a dedicated page which I believe needs to be loaded in order for the action to run. Additionally I don't know how to get the data from the form submission to the page on which the Send Email action runs.
How can I submit a Locomotive CMS content type form via AJAX with Google reCAPTCHA enabled and send an email containing the form submission data to the person who submitted in the form?
A couple of prerequisites:
Set up metafields_schema.yml to include the necessary information for sending emails:
Set up an email template:
email-template.liquid:
If the condition of using Google reCAPTCHA is removed, the task is relatively straight-forward.
We "simply" pass the form data to a custom page where we send an email and create a content entry using the Actions API.
Setup as follows:
Manually create a form to collect the content type data. Note the form action points to a custom page:
Submit the form with AJAX:
form-submit.js:
Send an email and create a content type entry using the Actions API:
page-with-send-email-and-create-content-entry-action.liquid:
If Google reCAPTCHA is required, the task is more complex.
I don't believe there is a way to manually create a form in Locomotive CMS with Google reCAPTCHA enabled, which means the above method won't work.
Google reCAPTCHA is available in Locomotive CMS via the default content type form setup:
Note: The property
recaptcha_requiredneeds to be set totruein the content_type yml file.In this case we cannot set a custom url for the form action. Additionally the reCAPTCHA verification means we need to let the form submit and create the content entry via the usual process and send the email separately.
To do this we will need to get the ID of the content entry created upon form submission. We can then run an additional AJAX request and pass the ID to a custom page containing a send email action. On the custom page we will use the ID to reference the content entry and get the data to populate the email.
Setup as follows:
Create form via default method above.
Submit the form with AJAX. Upon successful submission, get the content entry ID and pass it to a secondary AJAX request:
form-submit.js:
Find the submitted content entry and send an email using the Actions API:
page-with-send-email-action.liquid: