MVC框架中 ajax实现无刷新用户名验证

模板代码

<html>
<head>
<script type="text/javascript">
 function ajaxDate(){
 
 var xhr = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Msxm12.XMLHTTP");
 var username =document.getElementById('username').value;
 var url="index.php?c=user&a=ajax&time="+Math.random()+"&username="+username;
 xhr.open("GET",url,true);//打开地址栏
 //get 方式传递必须注意传递随机数避免有缓存。中文编码encodeURL() 解码URLencode()
 xhr.send();//
 xhr.onreadystatechange =callback;
  function callback(){
   //alert(xhr.readyState);
   if(xhr.readyState == 4){
    if(xhr.status == 200){//请求发送成功
     if(xhr.responseText == '1'){//获得服务器响应的数据
      alert('用户名存在');
     }
     else{
      alert('用户名申请成功');
     }
    }
   }
   }
 }
</script>
</head>
<body>
<table>
<caption>用户注册表</caption>
<tr>
 <td>用户名:</td>
 <td><input type="text" id="username" οnblur="ajaxDate()"></td>
</tr>
<tr>
 <td>密码</td>
 <td><input type="password" id="password"></td>
</tr>
<tr>
 <td>emil</td>
 <td><input type="text" id="username"></td>
</tr>
<tr>
 <td colspan="2"><input type="submit" id="submit"></td>
</tr>
</table>
</body>
</html>

 

控制器代码

//命令视图层显示模板
 //验证用户名
 public function regAction(){
  $user_model=new userModel('localhost','root','','yanhua');
  $this->smarty->display('user.tpl');
 }
 //命令模型层查询注册的用户名是否存在
 public function ajaxAction(){
  //这个动作判断用户名是否重复
  $user_model=new userModel('localhost','root','','yanhua');
  $row =$user_model->user();
  if($row){
   echo '1';
  }
  else{
   echo '0';
  }
 }

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值