您的位置 >>> 星想互联 >>> 编程技术 >>> PHP高级编程
thinkPHP+AJAX无刷新检测用户名
点击数:2989  发布时间2017-12-10 23:53:26

首先是引入JQUERY库,这是不可少的:

<script src="__PUBLIC__/jquery.min.js"></script>

然后,我们完成视图文件index.html,其中有表单和js:

表单中的B标签保存提示信息:

<form name="form1" method="post" action="">
    <label for="textfield"></label>
    <input type="text" name="textfield" id="adm"><b style="color:#f00" id="ad"></b>
    <label for="textfield2"></label>
    <input type="text" name="textfield2" id="pwd">
    <input type="submit" name="button" id="button" value="提交">
</form>

index.html页面JQUERY程序,U('Index/chkadm'),我们用U方法写入路径,对于$.post方法,咸宁网站建设公司有专门文章介绍

<script>
$(function(){
 $("#adm").blur(function(){
   $.post("{:U('Index/chkadm')}",{adm:$("#adm").val()},function(data){
    $('#ad').html(data);
   })
    })
})
</script>

最后,我们完成控制器IndexController.class.php,chkadm()实现检测功能,程序代码如下:

class IndexController extends Controller {
    public function index(){
   $this->display();
 }
 
 public function chkadm(){
  $adm=I('post.adm');
  $model=M('admin');
  $datas=$model->where(array('adminname'=>$adm))->find();
  if($datas){
   $txt="此用户已存在";
  }else{
   $txt="合法用户";
  }
  $this->ajaxReturn($txt); 
 }
}

来源:咸宁网站建设