标签:
<p>
请输入用户名:<input type="text" id="demo"/>
<span id="result"></span>
</p>
<script type="text/javascript">
document.getElementById("demo").onblur=function(){// 输入框失去焦点
var thisNode=this;
var span=document.getElementById("result");
var xmlhttp;
try{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=newXMLHttpRequest();
}catch(e){
// code for IE6, IE5
xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState<4){// 正在交互
span.style.color="blue";
span.innerHTML="正在检测...";
}
if(xmlhttp.readyState==4&& xmlhttp.status==200){// 请求成功
if(parseInt(xmlhttp.responseText)){// 将服务器返回的数据转换为整数
span.style.color="red";
span.innerHTML="抱歉,您填写的用户名已经存在!";
}else{
span.style.color="green";
span.innerHTML="恭喜你,填写正确!";
}
}
}
xmlhttp.open("POST","http://localhost/moyu.php?action=checkUserName",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("username="+thisNode.value);
}
</script>
<?php
if($_GET[‘action‘]=="checkUserName"){
// 为简单起见,这里不再查询数据库,使用 3 个用户名作为示例
if(
$_POST[‘username‘]=="admin"||
$_POST[‘username‘]=="xiaoming"||
$_POST[‘username‘]=="zhangsan"
){
echo 1;
}else{
echo 0;
}
}
?>
请输入用户名:
标签:
原文地址:http://www.cnblogs.com/moyuling/p/5272496.html