请求页面:ajax.html
php
<!doctype html>html
<html>ajax
<head>数据库
<meta charset="utf-8">ide
</head>spa
<body>htm
姓名:<input type="text" id="uname"/ ><span id="msg"></span><br/>对象
</body>事件
</html>ip
<script>
function $(id){
return document.getElementById(id); //根据指定的 id 属性值获得对象
}
$('uname').onkeyup=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>';
}
}