[英]jquery validate reCaptcha
我在使用jquery Validate插件验证此表单时遇到一些问题,当响应有效时,我只是没有得到任何响应,而且我也不知道为什么...这是html:
<form id="signin_form" action="" method="post">
<div id="recaptcha_widget" style="display:none">
<div id="recaptcha_image"></div>
<div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
<p><span class="recaptcha_only_if_image">Enter the <br />words above:</span>
<span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>
<span><input type="text" id="recaptcha_response_field" name="recaptcha_response_field" /></span></p>
<div class="get_another_captcha"><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div>
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>
<div class="help"><a href="javascript:Recaptcha.showhelp()">Help</a></div>
</div>
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=public_key">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=public_key"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<p><span></span><span> <input type="submit" value="Send" class="btn-submit" /> </span></p>
</form>
这是javascript:
$("#signin_form").validate({
rules: {
recaptcha_response_field: {
required: true,
remote: {
url:"verify.php",
type:"post",
async:false,
data: "recaptcha_challenge_field=" + $('#recaptcha_challenge_field').val() + "&recaptcha_response_field=" + $('#recaptcha_response_field').val()
}
}
},
messages: {
recaptcha_response_field: {
required: "*",
remote: "Invalid captcha"
}
}
});
而verify.php如下:
require_once('recaptchalib.php');
$privatekey = "private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
echo "false";
}
else{
echo "true";
}
有谁知道为什么吗?,我似乎无法找到我一直在努力解决的问题..,不知道为什么当验证码正确时什么都不会发生……在此先感谢
代码的问题在于,设置规则时会获取recaptcha_response_field
值,而不是在验证规则时获取。
固定代码:
$("#signin_form").validate({
rules: {
recaptcha_response_field: {
required: true,
remote: {
url:"verify.php",
type:"post",
async:false,
data: {
recaptcha_challenge_field: function(){ return $('#recaptcha_challenge_field').val(); },
recaptcha_response_field: function(){ return $('#recaptcha_response_field').val(); }
}
}
}
},
messages: {
recaptcha_response_field: {
required: "*",
remote: "Invalid captcha"
}
}
});
echo json_encode(true);
代替
echo 'true';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.