将reCAPTCHA响应更改为alert()而不是加载新页面

时间:2012-06-29 09:43:13

标签: php recaptcha

我正在使用reCAPTCHA,如果CATPCHA被错误填写为alert();而不是加载新页面,我想显示响应。我怎么能这样做?

这是表单操作:

<form id="form" method="POST" action="verify.php">

在verify.php文件中使用:

<?php
  require_once('recaptchalib.php');
  $privatekey = "(my key)";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

2 个答案:

答案 0 :(得分:3)

在表单提交上调用您的方法,如 -

<form id="form" method="POST" action="verify.php" onsubmit="mymethod()" >

或者使用jquery,类似的东西 -

<script>

    $("form").submit(function() {
      if ($("input:first").val() == "correct") {
        $("span").text("Validated...").show();
        return true;
      }
      $("span").text("Not valid!").show().fadeOut(1000);
      return false;
    });
</script>

答案 1 :(得分:0)

您可以在新页面上为警报编写Javascript。

if (!$resp->is_valid) {

  // What happens when the CAPTCHA was entered incorrectly
  echo("<script>alert('The reCAPTCHA wasn't entered correctly. Go back and try it again.');</script">);
  die();

} else {
  // Your code here to handle a successful verification
}