<html>
<head>
<title>用户注册</title>
<script type = "text/javascript" language = "javascript">
//创建ajax引擎
function getXmlHttpObject()
{
var xmlHttp=null;
//不同的浏览器获取对象xmlhttprequest 对象方法不一样
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
var myXmlHttpRequest="";
//验证用户名是否已经存在
function checkName(){
myXmlHttpRequest=getXmlHttpObject();
//判断创建是否成功
//如果使用post方式提交则用下面的代码
//建议使用post方法 因为 已经解决了中文乱码问题,可以传送大量数据
if(myXmlHttpRequest){
var url="registerprocess2.php";
var data1="userName1="+$("userName").value;
//打开请求
myXmlHttpRequest.open("post",url,true);
//必须加的一句话
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//指定回调函数 onreadystatechange状态改变事件触发器
myXmlHttpRequest.onreadystatechange=chuli;
//发送请求,如果是get请求 填写null即可
myXmlHttpRequest.send(data1);
}
}
//回调函数
function chuli(){
//window.alert("处理函数被调用"+myXmlHttpRequest.readyState);//readyState 对象状态
//取出从 服务器页面返回的数据
if(myXmlHttpRequest.readyState==4){
var mes=myXmlHttpRequest.responseText;
//使用eval函数将mes字串转成对应的对象
var mes_obj=eval("("+mes+")");
$('myres').value=mes_obj.res;
}
}
//封装一个方法
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<form action="???" method="post">
用户名字:<input type = "text" name = "userName" id = "userName">
<input type = "button" onclick = "checkName()" value = "验证用户名">
<input style = "border-width:0;color:red" type = "text" id = "myres">
<br/>
用户密码:<input type = "password" name = "password"><br>
电子邮件:<input type = "text" name = "email"><br/>
<input type = "submit" value = "用户注册">
</form>
<form action="???" method="post">
用户名字:<input type = "text" name = "userName2" id = "userName2">
<input type = "button" value = "验证用户名">
<input style = "border-width:0;color:red" type = "text" id = "myres2">
<br/>
用户密码:<input type = "password" name = "password"><br>
电子邮件:<input type = "text" name = "email"><br/>
<input type = "submit" value = "用户注册">
</form>
</body>
</html>
<?php
//这里两句话很重要,第一句话告诉浏览器返回的数据是xml格式
//header("Content-Type:text/xml;charset=utf-8");
//告诉浏览器不要缓存数据
//header("Cache-Control: no-cache");
//接收数据 POST || GET 根据前面的请求方式而定
$username=$_POST["userName1"];
$info="";
if($username=="zs"){
//这里是json数据 json是原生态的 数据格式非常稳定 描述能力很强
$info='{"res":"该用户不可以用","id":"A001","data":"2014-02-03"}';
}else{
$info='{"res":"该用户可以用"}';
}
echo $info;
?>