0

我在使用 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";
 }

有谁知道为什么?,我似乎无法找到我一直在努力尝试的问题..,不知道为什么当验证码正确时不会发生任何事情......提前非常感谢

4

3 回答 3

2

代码的问题在于它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"
        }
    }
});
于 2013-03-23T04:02:21.013 回答
1
echo json_encode(true);

代替

echo 'true';
于 2013-02-06T06:50:02.260 回答
0

问题是您没有使用来自verify.php.

这是获取响应和使用它们的教程。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.post/

于 2012-09-30T07:04:39.640 回答