需要在使用AJAX,jQuery .submit on按钮时返回recaptcha响应,并保持序列化数据不变

时间:2011-08-29 06:17:19

标签: php jquery ajax recaptcha .post

我到处寻找并试过了10个不同的类似问题,但两者都没有完全符合我的需要。

我有一个使用validationEngine进行验证的简单表单(这样可以正常工作,验证必需的字段和验证码字段,并且不允许jQuery .submit()函数处理直到它们有效)。这段代码应该收集recaptcha字段,通过AJAX发送到php处理程序,并接收响应,一直使用.submit()用于非侵入式代码:

使用Javascript:

function validateCaptcha() {
   challengeField = $("input#recaptcha_challenge_field").val();
   responseField = $("input#recaptcha_response_field").val();
   var html = $.ajax({
   type: "POST",
   url: form.attr('action'),
   data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
   async: false
   }).responseText;

   if(html != "Valid") {
     $("#captchaError").html('<p class="error">The security code you entered did not match. Please try again.</p>');
     $captchaFlag = "Invalid";
     Recaptcha.reload();
   } else {
     $("#memberInformation span").css({'color':'green'});
     $("#memberInformation span").html(html.message).show(3000);
     $("#captchaError").html('<p>Success!</p>');
     $captchaFlag = "Valid";
     dataString = form.serializeArray();
     getSearchMembers(dataString);
   }
}


   formID.submit(function() {
     var form = $(this);
     if (formID.validationEngine('validate')) { //if it validates
       $('#memberInformation span').html('');
       //Captcha check
     } //end if (formID.validationEngine('validate'))
     else { //if does not validate
       $('#memberInformation span').css('color','#ff0000').html('Please fill out required fields').show(3000);
     } //end else
     return false;
    }); //end formID.submit

PHP     

$privatekey = "1234567890"; //<!----- private key here
$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
echo "Error";
die ("The reCAPTCHA wasn't entered correctly. Please go back and try it again. (reCAPTCHA said: " . $resp->error . ")");
} else {
echo "Valid";
}
?>

除了将_post数据发送到php文件外,一切正常,因此它总是响应为“无效”。有任何想法吗。我想使用.submit()和AJAX,以及当前的验证器。否则任何其他东西都可以改变它所需要的一切。

2 个答案:

答案 0 :(得分:2)

您可以使用:

index.php(您的CAPTCHA出现的页面)

<html>
    <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
      <!-- your HTML content -->
<script type="text/javascript">
function showHint()
{
var xmlhttp;
var x=document.form.recaptcha_challenge_field.value;
var y=document.form.recaptcha_response_field.value;
if (x.length==0)
  {
  document.getElementById("hint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("hint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+x+"&r="+y,true);
xmlhttp.send();
}
</script>
 <script type="text/javascript">
 var RecaptchaOptions = {
    theme : 'white'
 };
 </script>
      <form name="form">
        <?php
          require_once('recaptchalib.php');
          $publickey = "1234567"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?><br><br><p id="hint"></p>
        <input type="button" value="check" onclick="showHint()" />
      </form>

      <!-- more of your HTML content -->
    </body>
  </html>

gethint.php这会验证验证码

<?php
  require_once('recaptchalib.php');
  $privatekey = "your private keys";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_GET["q"],
                                $_GET["r"]);

  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 {
  echo "Correct";
  }
  ?>

别忘了更换私钥和公钥。

答案 1 :(得分:1)

首先,在服务器端转储您的发布数据,以查看是否已设置所有值。另外,在浏览器中验证ajax请求实际发送的内容。在Chrome中,您可以在“网络”标签的开发者面板中查看此内容。看看你的帖子是否正确。此外,检查您是否已将正确的密钥放在正确的位置,我曾将公钥放在私有变量上,反之亦然。