如何在表单提交后重置recaptcha?

时间:2017-08-14 08:47:15

标签: javascript php recaptcha invisible-recaptcha

这是我的PHP代码:

    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();
}}

,这是我的JavaScript代码:

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

这是我的验证码:

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

,这是按钮提交:

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

这也是我的表单ID:

<form id="i-recaptcha">

**

  

以上功能正常,但提交表格后仍然是验证码   如果用户点击提交,我想将其重置为未选中状态   按钮。

**

提前致谢

1 个答案:

答案 0 :(得分:9)

你可以像这样使用它。它基于版本。

版本2

grecaptcha.reset();

版本1

Recaptcha.reload();