0

I have a working form with captcha. When I load up the webpage and click on submit without checking the box , the alert shows (click on robot), but then if I let the captcha expire and click submit again, the alert doesn't pop up any more. Is there something wrong in the following code or any way to rewrite it?

<script>
    window.onload = function() {
    var recaptcha = document.forms["form"]["g-recaptcha-response"];
    recaptcha.required = true;
    recaptcha.oninvalid = function(e) {     
        alert("Please click on i am not a robot");
        }
    }
</script>

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

<div class="g-recaptcha" data-sitekey="xxx" data-callback="recaptcha"></div>

Please take a look at my server side php. I tried to add data expired callback several times in past few days but couldn't make it work.

$recaptcha_secret = "xxx";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true) {

                mail($to, $subject, $body, "From:" . $email);

        }
        else
        {

        return false;

        }
        echo 'success';
    }
whatsup
  • 29
  • 1
  • 9

1 Answers1

0

Re-captcha provides callback handlers when ever user select the checkbox and when checked token got expired. for e.g

 <div class="g-recaptcha" id="headerCaptcha" data-callback="recaptchaCallbackHeader" data-expired-callback="recaptchaExpiryHeader" data-sitekey="xxx"></div>
 // There should be expiry handler like 
function recaptchaExpiryHeader(){
    alert('You should have to check captcah again as it is expired');
}  

More about the same

manikant gautam
  • 3,521
  • 1
  • 17
  • 27
  • thank you for your reply. I have checked your other post already. no matter what I did, I couldn't get the expired alert to show . Can you please show me how to fix the issue. Thank you for your time. @manikant – whatsup Jan 20 '20 at 22:40