ajax.js
var HTTP_Request=false;
function send_request(url){url = url + "&rand=" + new Date().valueOf();
HTTP_Request=false;
//开始初始化XMLHttp对象
if(window.XMLHttpRequest){//MOILLA浏览器
HTTP_Request=new XMLHttpRequest();
if(HTTP_Request.overrideMimeType){
HTTP_Request.overrideMimeType("text/xml");
}
}else if(window.ActiveXObject){//IE 浏览器
try{
HTTP_Request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
HTTP_Request=new ActiveXObject("Microsoft.XMLHttp");
}catch(e){
alert("浏览器不支持此方法,请更换浏览器!");
return false;
}
}
}
if(!HTTP_Request){
window.alert("创建XMLHttp对象失败");
return false;
}
HTTP_Request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
HTTP_Request.open("GET",url,true);
HTTP_Request.send(null);
}
//处理返回信息函数
function processrequest(){
if(HTTP_Request.readyState==4){
if(HTTP_Request.status==200){
document.getElementById(reobj).innerHTML=HTTP_Request.responseText;
}else{
alert("您所请求的页面不正常");
}
}
}
function do_ajax(obj,url){
document.getElementById(obj).innerHTML="查询中...";
send_request(url);
reobj=obj;
}
reg.html
<html>
<head>
<title>ajax测试</title>
<script src="ajax.js"></script>
</head>
<body>
假设已经注册过的用户名有tom,leo,lili,richard
<br />
<br />
用户名:
<input type='text' name='username' onblur='do_ajax("result","check.PHP?username=" + this.value)' />
<div id="result"></div>
</body>
</html>
check.PHP
<?PHP //链接数据库 $link = MysqL_connect('localhost','root','123') or die('连接数据库失败,系统给的提示是:'.MysqL_error()); //选择数据库 MysqL_select_db('opp_blog') or die ('选择数据库失败,系统给的原因是:'.nysql_error()); $sql ="select * from opp_user"; //echo $sql; $result = MysqL_query($sql) or die(MysqL_error()); //查询数据 #while($username=MysqL_fetch_array($result)){ #echo "<pre/>"; #var_dump($username['username']); #} $usernames =MysqL_fetch_array($result); $names = array('tom','leo','lili','richard'); if($_GET['username']){ sleep(2);//这一句是为了展示查询过程值得变化,下面也是模拟查询过程,实际你可以到数据库查询 if(in_array($_GET['username'],$usernames)){ echo '此用户名已存在'; }else{ echo '恭喜,可以使用'; } }else{ echo '请输入用户名'; } ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。