请求页面:ajax.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
姓名:<input type="text" id="uname"/ ><span id="msg"></span><br/>
</body>
</html>
<script>
function $(id){
return document.getElementById(id); //根据指定的 id 属性值得到对象
}
$('uname').οnkeyup=function(){
//在键盘按键被松开时触发事件
var n = $('uname').value; //将对象的值赋给新的变量
var x = new XMLHttpRequest(); //创建ajax请求对象
x.open('GET','ajax.php?n='+n,true); //设置传送方式和路径
x.onreadystatechange=function(){ //
if(x.readyState==4 && x.status==200){
$('msg').innerHTML = x.responseText; //在ID为msg的标记中载入获取的数据
}
}
x.send();//发送数据
}
</script>
响应检测页面:
<?php
if(isset($_GET['n'])&&$_GET['n']!=""){
$n = $_GET['n'];
if($n=='admin'){ //用admin代表数据库查询到的值
echo '<font color=red>已经存在</font>';
}else{
echo '<font color=green>不存在,可以使用</font>';
}
}
转载于:https://blog.51cto.com/jinkaiye/1357556