无需Google Recaptcha v2验证即可提交联系表单

时间:2019-04-08 15:23:49

标签: javascript php forms recaptcha

我在php中使用了联系表格,并在其上添加了Google recaptcha。但是我可以提交表格而无需重新验证。我如何才能根据需要进行此验证码。我已经尝试了一些解决方案,但是没有一个对我有用。感谢您的回答,谢谢

<form name="contact" method="post" action="" id="fsrep-contact-form">
  <table cellspacing="0" cellpadding="1" border="0">
    <tr id="fname">
      <td>First Name <span>*</span></td>
      <td><input type="text" name="fname" required></td>
    </tr>
    <tr id="lname">
      <td>Last Name</td>
      <td><input type="text" name="lname"></td>
    </tr>
    <tr id="emailid">
      <td>Email <span>*</span></td>
      <td><input type="email" name="email" id="mail" required></td>
    </tr>
    <tr id="phone-no">
      <td>Cell Phone <span>*</span></td>
      <td><input type="number" name="phone" required></td>
    </tr>
    <tr>
      <td>Please give us any other details you would like, that would help us to help you find your next home</td>
      <td><textarea name="message"></textarea></td>
    </tr>
    <tr>
      <td>
        <div class="g-recaptcha" id="rcaptcha" name="googlecaptcha" required data-sitekey="6LffZpsUAAAAAEC_KyN4KauhSSmKdRf9SVR5aVJD" data-callback="correctCaptcha"></div>
      </td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" value="SUBMIT INQUIRY" id="submit"></td>
    </tr>
  </table>
</form>

1 个答案:

答案 0 :(得分:0)

我已经尝试过了,它对我有用:

您具有“数据回调”属性

<div class="g-recaptcha" id="rcaptcha" name="googlecaptcha" required data-sitekey="YOUR KEY" **data-callback="correctCaptcha"**></div>

只需创建一个名称为 correctCaptcha

的函数
var recaptchachecked=false; 
function correctCaptcha() {
    recaptchachecked = true;
}

然后将其保存在表单中,直到验证recaptchachecked变量是否为真,然后继续。

<form method="post" onsubmit="return isreCaptchaChecked(event)">
..
..
</form>

function isreCaptchaChecked()
{
  e.preventDefault();
  return recaptchachecked;
}

希望对您有帮助。