无法获得验证码工作的jquery验证

时间:2014-09-18 13:24:08

标签: php jquery jquery-validate

我一直在尝试实时验证验证码,我使用的是jquery验证版本1.11.1。 我已将验证码的值存储在会话变量名称security_code中。我正在使用jquery的远程方法。我是第一次使用它(jquery验证插件,它仅用于学习目的)。

这是我的代码,显示了我的尝试。

的index.html

<label class="field">
<input type="text" name="securitycode" id="securitycode" class="gui-input sfcode" placeholder="Enter code">
</label>

validfile.js

securitycode:{
                required:true,
                remote: {
                url: "a.php",
                type: "post"
            },
messages:{
          securitycode:{
                required: 'Please enter security code',
                remote: 'Please enter correct security code'
                }
         }

a.php文件

<?php
if(isset($_POST['securitycode']))
{
    if($_POST['securitycode']==$_SESSION['security_code'])
    {
        exit("true");
    }
    else exit("false");
}
exit();
?>

注意:我已经包含了所有有效文件,也是正确的目标

提前致谢

2 个答案:

答案 0 :(得分:0)

使用echo代替exit

<?php
if(isset($_POST['securitycode']))
{
    if($_POST['securitycode']==$_SESSION['security_code'])
    {
        echo "true";
    }
    else echo "false";
}
exit();
?>

提示

如果一切设置正确并且remote正在触发,您将能够从浏览器控制台查看服务器响应。否则,请检查控制台是否有错误。


修改

我认为此代码在.validate()方法中,但是,您似乎缺少一些大括号和事物......

$('#yourForm').validate({ // <- you did not show us this part?
    rules: { // <- this was missing, maybe you just forgot it when posting the question
        securitycode: {
            required:true,
            remote: {
                url: "a.php",
                type: "post"
            }  // <- this was missing, closing brace for 'remote'
        }      // <- this was missing, closing brace for 'securitycode'
    },         // <- closing brace for 'rules'
    messages: {
        securitycode: {
           required: 'Please enter security code',
           remote: 'Please enter correct security code'
        }
    },
    // other options and callbacks
});

答案 1 :(得分:-1)

我解决了它,愚蠢我总是忘记添加session_start();在我的PHP文件中。很抱歉浪费你的时间。  所以解决这个问题的方法是添加session_start();在a.php中