//输出login页面
public function login(){
if(!IS_POST){
$this->display('Home/common/header');
$this->display();
$this->display('Home/common/footer');
}else{
// var_dump($_POST);
// 如果输入验证码正确
echo $this->chCode();
if($this->chCode()!=='0'){
echo '验证码不正确';
}else{
echo '验证码正确';
.........继续下面业务..........
}
}
//验证验证码是否输入正确
public function chCode(){
if(IS_POST){
$checkcode = I('post.checkcode');
if($this->check_verify($checkcode)==false){
return '1';
}else{
//0为验证码正确
return '0';
}
}
}
//输出给js文件实现ajax提示验证码对错
public function ajaxCode(){
print_r($this->chCode());
}
html文件:
<form action="" method="post">
<i class="fa fa-user" id="iUser"></i>
<i class="fa fa-key" id="iKey"></i>
<input type="text" name="user" placeholder="账户" id="user" autofocus>
<input type="password" name="" placeholder="密码" id="password">
<input type="text" name="checkcode" placeholder="验证码" id="checkcode">
<img src="{:U('verify',array())}" id="imgcheckcode" >
<button type="submit" >登陆</button>
<a href="">找回密码></a>
<a href="">免费注册></a>
</form>
<div class="prompt">
<span class="checkerror" >验证码错误</span>
<span class="checktick" >✔</span>
</div>
js文件:
//ajax检查验证码
$('#checkcode').blur(function(){
var data={};
data.checkcode=$(this).val();
var url="ajaxCode";
//console.log(data);
$.post(url,data,function(e){
//e返回0或1
console.log(e);
if(e==1){
// 1 为验证码输入错误
$('.checkerror').css('display','inline');
$('.checktick').css('display','none');
}
if(e==0){
// 0 为验证码输入正确
$('.checkerror').css('display','none');
$('.checktick').css('display','inline');
}
});
});
问题来了,输入验证码blur后,页面显示验证码正确,console也输出0,但是按提交后页面最上面输出 $this->chCode()为1 同时输出'验证码不正确',这是为啥呢? 下面业务没法走了
最佳答案