我一直在尝试实时验证captcha,我使用的是jquery验证版本1.11.1。我已经将captcha的值存储在会话变量securitycode中。我使用的是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();
?>
注意:我已经包括了所有有效的文件,我也是正确的目录
提前感谢
使用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
});
我解决了它,愚蠢的我总是忘记添加session_start();在我的php文件中。抱歉浪费了你们的时间,伙计们。因此,解决方案是添加session_start();在a.php中