模板代码
<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';
}
}