ajax get发送测用户名是否存在
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajax</title>
<script src="ajax.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
注册用户名:
<input name="user" type="text" id="user" onblur="send_url('action.php?action=checkuser&user='+document.getElementById('user').value,'user_error')" /><span id="user_error"></span>
</form>
</body>
</html>
ajax.js页面
// JavaScript Document
function newXMLHttpRequest() {
var http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
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) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
return http_request;
}
function send_url(url,obj)
{
//cheack(obj);
var http_request = newXMLHttpRequest();
http_request.onreadystatechange = processRequest;
http_request.open("GET", url, true);
http_request.send(null);
// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4 || http_request.readyState == 0) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById(obj).innerHTML=http_request.responseText;
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
}
action.php页面
<?php
$action=$_REQUEST['action'];
if($action=='checkuser')
{
$user=trim($_GET['user']);
if(!empty($user))
{
$arr=array('a','b','c','d');
for($i=0;$i<count($arr);$i++)
{
if($user==$arr[$i])
{
$flag=1;
break;
}
else
{
$flag=0;
}
}
if($flag==1)
{
echo "<font color=red>对不起,该用户已存在!</font>";
}
else if($flag==0)
{
echo "<font color=red>恭喜,该用户可以使用!</font>";
}
}
else
{
echo "<font color=red>用户名不能为空!</font>";
}
exit();
}
?>