Recaptcha v3 Integration

16 replies · opened Dec 21, 2020

SsguvenDec 21, 2020

Hi,

I tried to implement Google's recaptcha v3 to one of your forms by adding the following script. However, when the form.submit(); is called, the form is submitted to another page. So it is not submitted through your AJAX implementation and I don't see the green response message on the top-right corner of the screen. How can I have recaptcha with your forms?

Thanks,
Serhat

<script>
grecaptcha.ready(function() {
$('#widget-subscribe-form').submit(function(e) {
var form = this;
e.preventDefault();
grecaptcha.execute('6LdMfw8aAAAAAD3CoHZK0b6HOjIRjtSYB2J5szs7', {action: 'submit'}).then(function(token) {
$('#id_recaptcha').val(token);
console.log('Token: ' + token);
form.submit();
});
});
});
</script>

SSemicolon WebSTAFFDec 22, 2020

Hello,

This is because you are using Custom JS Codes for the Form which overrides the Default Functionality. Meanwhile, we already have an Example included for Google reCaptcha v3: http://themes.semicolonweb.com/html/canvas/contact-recaptcha-v3.html . You can simply change the YOUR_SITE_KEY to your v3 Site Key and add your v3 Secret Key in the include/forms.php File.

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 22, 2020

Hi,

I just removed the form.submit(); line and it worked. Thanks!

SSemicolon WebSTAFFDec 22, 2020

Hello,

Glad the issues were resolved. We would still recommend using the Default Codes as this might cause conflicts later down the line because of this line: $(‘#widget-subscribe-form’).submit(function(e).

Do let us know if we can help you with anything else or if you find any further issues with Canvas.

SsguvenDec 22, 2020

Your solution is based on the assumption that I use PHP but I don't. I think you should flexible JS codes that work with all back-end types.

SSemicolon WebSTAFFDec 22, 2020

Hello,

We actually didn't know which Backend Type you were using. Our JS Codes are flexible enough to work with most backend languages. What we meant with our suggestion was for you to make sure that both the JS Functionalities do not conflict with each other. Because if you are using the same JS Function on a Single Element, they would not work like default and cause conflicts. So to extend it, you can add the .customjs Class to the .subscribe-widget DIV and this would disable the Default JS Functionality, and then you can use your Custom Codes ensuring that everything will work smoothly.

We are always here to help and provide you with the best possible recommendations.

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 23, 2020

Ho can I implement this for the email bulletin subscription form at the bottom of the mage for the Medical template?

SSemicolon WebSTAFFDec 24, 2020

Hello,

Apologies for the Inconvenience regarding this.

This is a Silly Mistake on our side, as we had missed adding the .subscribe-widget Class on the .widget DIV wrapping the Subscription Forms. Simply add this class and this issue will be resolved!

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 24, 2020

Yes I know. But that's not the problem because I already have that class.

So, I am checking the http://themes.semicolonweb.com/html/canvas/contact-recaptcha-v3.html along with the /includes/form.php per your suggestion. So html template doesn't seem to post the required variable $submits['g-recaptcha-response'] to /includes/form.php to verify from Google. Am I missing something? Because in my case I get the error 'missing-input-response' from Google.

SSemicolon WebSTAFFDec 24, 2020

Hello,

The http://themes.semicolonweb.com/html/canvas/contact-recaptcha-v3.html Template uses the include/form.php File, while the Subscription Widget uses the include/subscribe.php File. You will need to add the Google reCaptcha PHP Codes in the this File as well to make it work with Subscription Widgets.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 24, 2020

Well it didn't help. It doesn't matter because when I fill in the form at http://themes.semicolonweb.com/html/canvas/contact-recaptcha-v3.html following are the variables posted and none of them are related to captcha.

template-contactform-name=Tester&template-contactform-email=test%40test.com&template-contactform-phone=&subject=Test&template-contactform-service=&template-contactform-message=Test&template-contactform-botcheck=&prefix=template-contactform-&template-contactform-submit=submit

Would you please let me know how I can implement the recaptcha-v3 to ANY form in your template?

SSemicolon WebSTAFFDec 24, 2020

Hello,

Apologies for the Inconvenience! There was a Slight Bug in the Code in the HTML. Find the following code:
[ch_pre]


grecaptcha.ready(function() {
grecaptcha.execute('YOUR-SITE-KEY', {action: 'contact_page'});
});

[/ch_pre]

and replace it with:
[ch_pre]

grecaptcha.ready(function() {
	grecaptcha.execute('YOUR-SITE-KEY', {action: 'contact_page'}).then(function(token) {
		// add token value to form
		document.getElementById('g-recaptcha-response').value = token;
	});
});

[/ch_pre]

and then you simply add your reCaptcha v3 Secret in the include/form.php File. If you are using the Subscription Forms, you will need to Copy the reCaptcha related Codes from the include/form.php File and paste it in the include/subscribe.php File.

This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 24, 2020

Great, that worked! Many thanks!

May I also ask if it would work when I have more than 1 forms in a single page? Both should be captcha-protected.

SSemicolon WebSTAFFDec 24, 2020

Hello,

This would work perfectly fine for multiple forms on the Page. just change the 2 marked IDs (see attached image) per form.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 24, 2020

Do you think I can use Jquery and set the value of all these inputs in different forms by using a class selector of the JQuery? I mean, the scripts in your suggestion don't need to be with the <form> tag, right? Only the hidden inputs needs to be there.

PS. I'm not using the form.php of yours, so I will be handling each form separately with Django.

SSemicolon WebSTAFFDec 24, 2020

Hello,

It would be more recommended to use it this way as the reCaptcha needs to set token after being executed.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

SsguvenDec 24, 2020

Got it. Thanks a lot!

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard