I am trying to configure re captcha for contact us form and newsletter form.
Both these form are on one page.
Now I have to configure common recaptcha for these two form and when on submit captcha should work for both forms.
I have tried to configure programmatically for both form but it is not working me. According to me captcha gives conflicts on form submission
I have following code for news letter form:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("newsletter-validate-detail").submit();
}
</script>
and
<button class="g-recaptcha action subscribe primary" data-sitekey="SITE_KEY" data-callback='onSubmit' title="<?php /* @escapeNotVerified */ echo __('Subscribe') ?>" type="submit">
<span><?php /* @escapeNotVerified */ echo __('Subscribe') ?></span>
</button>
for the contact-us form:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("contact-form").submit();
}
</script>
and
<button class="g-recaptcha action submit" data-sitekey="SITE_KEY" data-callback='onSubmit' title="<?php /* @escapeNotVerified */ echo __('Subscribe') ?>" type="submit">
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
</button>
is it possible to add recaptcha for multiple forms on same page programmatically?
Best Answer
You could use this extension https://github.com/magento/magespecialist_ReCaptcha
And my pull request to have multiple ReCaptcha on a page
Some customization to get it work on newsletter on footer. Basically you'd need to add configuration and render it to footer where you newsletter form was.
system.xml
The model Config.php
And a function
The helper Data.php
The model LayoutSettings.php
Create a new template footer/captcha.phtml
Assume you have your footer newsletter form, call the block of recaptcha under your form
From Block/Frontend/Recaptcha.php add new function