【问题标题】:recaptcha 2 returns false but still submits fomrecaptcha 2 返回 false 但仍提交 fom
【发布时间】:2017-12-26 11:52:06
【问题描述】:

如果recaptcha 返回false,我目前正试图阻止表单提交,但它仍然提交表单。我也使用了 e.preventDefault ,但它不起作用,所以一定是不太正确。

提前感谢您的帮助。

查询:

submit: function() {
$form = $('#registration-form');
$form.submit(function (e) {
$output = _validate($form);
  if($output == false) {
    e.preventDefault(e);
  } else {
    $.ajax({
      type: "POST",
      url: "http://" + window.location.hostname + "/wp-content/themes/Listex/includes/plugins/forms/recaptcha.php",
      async: false,
      data: "g-recaptcha-response=" + grecaptcha.getResponse(),
      success: function(response) {
        alert(response);
        if (response == "false")
          return false;
      }
    });
}
});

},

recaptcha.php:

    <?php

$secret="secret code";
$response = $_POST["g-recaptcha-response"];

$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success = json_decode($verify);

echo json_encode($captcha_success->success);

【问题讨论】:

  • 你不能从异步回调函数返回任何东西
  • 对不起,这是我在 stackoverflow 上看到的东西,如果没有尝试仍然无法正常工作

标签: jquery ajax return recaptcha


【解决方案1】:

排序后将其与当前验证相关联:

jQuery:

if(output == true) {
    $.ajax({
      type: "POST",
      async: false,
      url: "http://" + window.location.hostname + "/wp-content/themes/Listex/includes/plugins/forms/recaptcha.php",
      data: {
        //THIS WILL TELL THE FORM IF THE USER IS CAPTCHA VERIFIED.
        captcha: grecaptcha.getResponse()
      },
      success: function( data ) {
        console.log("FORM SUCCESSFUL");
        output = data;
      },
    });

  }

  if ( !output ) alert( 'Please ensure all required fields have been entered' );

  return output;

recaptcha.php:

<?php

if(isset($_POST['captcha']) && !empty($_POST['captcha'])){

$secret="6Lej1CkUAAAAAP0ACyvliPJo7Tp5I2eH52C-rsfG";
$response = $_POST["captcha"];

$verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success = json_decode($verify);

echo json_encode($captcha_success->success);

}
?>

【讨论】:

    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多