-2

I would like to submit data using Jquery and ajax and do some stuff.

Trying Google Recaptcha V3. In the following code, on the line the alert ("token 2 is " + Mytoken); ---> here i get an error saying Mytoken isn't defined

The thing is how do i get grecaptcha.execute to return data that I can use elsewhere? For example, if the captcha worked, then I submit some ajax data with the token code and on the submitted backend script, validate the token there.

<html>
        <head>
            <title>Recaptcha Ajax</title>
            <script src="https://www.google.com/recaptcha/api.js?render=<?php echo $captcha_site_key; ?>"></script>
        </head>
        <body>



          <script>
          var MYtoken;
          grecaptcha.ready(function() {
              grecaptcha.execute('<?php echo $captcha_site_key; ?>', {action: 'test_page'}).then(function(token) {
                //$("#pet_token").val(token);
                //$('myform').prepend('<input type="hidden" name="recaptchaTOKEN" value="' + token + '">'); 


                alert ("token is " + token);
                alert ("and Mytoken is " + token);
              });
          });
        for (var i = 1; i <= 5; i++) {
            var tick = function(i) {
                return function() {
                    console.log(i);
                }
            };
            setTimeout(tick(i), 1000 * i);
        }
                alert ("token 2 is " + Mytoken);
          </script>
Rajan
  • 27
  • 1
  • 1
  • 4

1 Answers1

0

I figured it out... irrespective of whether I submit some data in Ajax or not, what matters is that the received data is validated with re-captcha

So... the ajax will simply set the value in a hidden field and on the backend page, I do the captcha validation

Google Recaptcha v3 example demo

Rajan
  • 27
  • 1
  • 1
  • 4