html 部分
<input type="text" id="code" class="code" name="code" placeholder="验证码">
<img src="__CONTROLLER__eck_code" alt="点击刷新" onclick='this.src=this.src+"?"+Math.random();' width="75" height="25"/>
jquery ajax 部分
<script type="text/javascript">
$(function(){
var path=window.location.hostname;
$("#code").blur(function(){
var code=$('#code').val();
jQuery.ajax({
type : 'POST',
contentType : "application/x-www-form-urlencoded",
url :'http://'+path+ '/anka/index.php/Admin/Index/check_code',
data : {'code' : code},
dataType : 'json',
success: function(data,response){
var resualt=data.success;
if(code==1){
$('<div id="repson" />').html("<font color=red>验证码不正确</font>").appendTo('#responseMsg').fadeOut(2000);
$("#code").select();
return false;
}
else if(resualt==0){
return true;
}
}
});
});
});
</script>
PHP部分
common目录下建立一个function
function check_verify($code, $id = 1,$reset = true){
$Verify = new \Think\Verify();
$Verify->reset=false;
return $Verify->check($code, $id);
}
引用了Controller下
public function check_code(){
if (IS_AJAX) {
if (check_verify(I('code'))) {
$arr['success'] = 1;
$array=json_encode($arr);
print_r($array);
}else {
$arr['success'] = 0;
$array=json_encode($arr);
print_r($array);
}
}
}
<input type="text" placeholder="验证码" name="code" class="code" id="code">
<img width="75" height="25" onclick="this.src=this.src+"?"+Math.random();" alt="点击刷新" src="/anka/index.php/Admin/Index/check_code">
图片 src 如何应用function里面的验证码函数啊 最佳答案