0

I am trying to implement Google recaptcha V3 with pure javascript and PHP, because I don't want to use heavy jQuery, and I don't know much about it.

But I didn't understand what 'execute()' method was while implementing it:

window.addEventListener('DOMContentLoaded', function()
    grecaptcha.execute('XXXXXXXXXXXXXXX', {action: 'homepage'}).then(function(token) {
        document.getElementById('g-recaptcha').value = token;
      });
});

I replaced '.ready()' method with addEventListener(), but I could not find what 'execute()' is.

Also I cannot get to know what 'grecaptcha' is, which was shown on the Google's example. (I didn't find where it was declared yet, on the example)

My Error (in javascript console) is :

Uncaught ReferenceError: grecaptcha is not defined

My form is defined like this:

<form method="post" action="reaction.php" id="tox" name="frm1">
   ...
   <input type="hidden" id="g-recaptcha" name="g-recaptcha">
   ...
</form>

My HTML header is defined like this:

<header>
      <script src="https://www.google.com/recaptcha/api.js?render=xxxxxxxxxxx"></script>
</header>

What is wrong in the code and what code should I add to declare 'grecaptcha'?

Thanks for your answer in advance.

Ashram Kane
  • 288
  • 2
  • 7
  • 17
  • first include the recaptcha api.js script in head tag and then include script tag containing your code. You can use the ready event of recaptcha, its not jQuery but recaptcha's own function. – Bharat D Bhadresha Mar 19 '20 at 08:49
  • you might also want to double check the open and close brackets so that they match. Maybe it was just when you pasted your code into this question but the javascript function don't have correct brackets – pastaleg Mar 19 '20 at 08:50
  • Thanks for your answers, now I encounter the error: The reason seems that because I am using the command : require "myform.php". I will try to combine two php files. – Ashram Kane Mar 19 '20 at 09:00
  • It seems to be an issue of site protocol difference. My site now does not support https, can't I use Google recaptcha? – Ashram Kane Mar 19 '20 at 09:19
  • You can use Google Recaptcha without ssl but I would recommend you to use SSL on your website. You can use letsencrypt.org for a free SSL. – Bharat D Bhadresha Mar 19 '20 at 09:55

0 Answers0