0
votes

i am using google recaptcha v3 in my website ,

here is the code i have added in my html web page in the head:

<script src="https://www.google.com/recaptcha/api.js?render=my public key"></script>

<script>
               grecaptcha.ready(function () {
               grecaptcha.execute('my public key', { action: 'contact' }).then(function (token) {
                   var recaptchaResponse = document.getElementById('recaptchaResponse');
                   ecaptchaResponse.value = token;
                       });
               });
</script>

and i added this in my form :

<input type="hidden" name="recaptcha_response" id="recaptchaResponse">

but when i submit the form and try to see the content of $_POST['recaptcha_response'] with var_dump() it is just an empty string : string(0) ""

where is the mistake ? thanks for answers !

2

2 Answers

0
votes

i tried lots of changes but everytime i get " token = null " then i try this.. 100% working....

in Your Html

<html>
<head>
</head>
<body>
  <form id="contact">
    <div class="col-12">
      <div class="form-group">
          <input type="hidden" name="captcha_token" id="recaptchaResponse" data-sitekey="YOUR-SITE-KEY">
      </div>
    </div>
    <div class="col-12">
        <button type="submit" class="btn btn-lg" id="submit-btn">
            SUBMIT
        </button>
    </div>
  </form>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="https://www.google.com/recaptcha/api.js?onload=ReCaptchaCallbackV3&render=YOUR-SITE-KEY"></script>
</body>
</html>

after then add in your js file

$(document).ready(function(){
  setInterval(function(){
        ReCaptchaCallbackV3();    
    }, 90000);
    
    $(document).on("submit",'#contact',function (e) {
      var token = $('[name="g-recaptcha-response"]').val();
      console.log(token);
    });
});

var ReCaptchaCallbackV3 = function() {
    grecaptcha.ready(function () {
        grecaptcha.execute('YOUR-SITE-KEY', { action: 'contact' }).then(function (token) {});
    });
}
0
votes

In your original script you have a typo:

var recaptchaResponse = document.getElementById('recaptchaResponse');
ecaptchaResponse.value = token;

Instead of recaptchaResponse, you wrote ecaptchaResponse