How to reset Google reCaptcha?

17,054

You can use it like this. It's based on version.

Version 3

grecaptcha.ready(function() {
    grecaptcha.execute('SITE_KEY', {action: 'homepage'}).then(function(token){
        $('#token').val(token);
    });
});

Version 2

grecaptcha.reset();

Version 1

Recaptcha.reload();
Share:
17,054
James
Author by

James

Updated on June 15, 2022

Comments

  • James
    James about 2 years

    Here is my PHP Code:

        function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }
    $res = post_captcha($_POST['g-recaptcha-response']);
    if (!$res['success']) {
          die('MF005');
    } else {
    if ($mail->send()) { 
    $send_arEmail = $autoresponder->Send();
    }}
    

    and here is my JavaScript code:

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
    function onSubmit(token) {
        document.getElementById("i-recaptcha").submit();
    }
    </script>
    

    This is my captcha:

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

    and this is button submit:

    <button class="button rounded" type="submit" data-callback="onSubmit">Send</button>
    

    This is also my form id:

    <form id="i-recaptcha">
    

    **

    This above function is working, but after submit form, still captcha is checked, I want to reset it to unchecked once user click on submit button.

    **

    Thanks in advance

  • James
    James almost 7 years
    I've added these two JavaScriptbut it doesnt work. How can I add it?
  • Bhautik
    Bhautik almost 7 years
    which versoin are you using?
  • James
    James almost 7 years
    I don't know which version, you can see my above codes. please give me a solution for this. Thank you
  • SHENE
    SHENE almost 5 years
    How to do this in version 3?
  • Bhautik
    Bhautik almost 5 years
    @Zam85 I think this grecaptcha.reset(); can work for you.
  • Bhautik
    Bhautik almost 5 years
    @Zam85 Please visit this developers.google.com/recaptcha/docs/faq#blocked_frame for more information about v2 vs v3.