html页面:
<html>
<head>
<title>ajax</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script>
function check(){
//生成XMLHttpRequest对象
function getXmlHttp(){
var XmlHttp;
if(window.XMLHttpRequest) XmlHttp = new XMLHttpRequest();//IE7+、Firefox、Chrome...
else XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE6以下版本
return XmlHttp;
}
var xmlhttp = getXmlHttp();
//get方法提交
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)document.getElementById("notice").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("GET","check.php?name="+document.getElementById("name").value,true);
xmlhttp.send();
//post方法提交
//xmlhttp.open("POST","check.php",true);
//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//xmlhttp.send("fname=Bill&lname=Gates");
}
</script>
</head>
<body>
<form>
用户名:<input type="text" id="name" onkeyup="check(); " value=""/><span id="notice"></span><br/>
昵 称:<input type="text" id="aliasName" /><br/>
<input type="button" value="提交"/>
</form>
</body>
</html>
check.php页面
<?php
$link = mysql_connect("localhost","root","123456");
mysql_select_db("test");
$name = $_GET["name"];
$result = mysql_query("select name from user");
$i = 0;
while($arr = mysql_fetch_array($result)){ $res[$i] = $arr['name'];$i = $i+1;}
//var_dump($res);
//$arr = array("rmzj","rmzjie","ybrmzj","rmzjlm","yubiao");
//header('Content-Type:text/html;charset=GB2312');
if(in_array($name,$res))echo "<span style=\"color:red;\">用户名已存在</span>";
else echo "恭喜,用户名可用!";
?>
来源:https://www.cnblogs.com/yilv/archive/2013/04/16/3024117.html