1、ajax.js文件
// javascript Document
var xmlHttp;
function S_xmlhttprequest()
{
xmlHttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getName(name)
{
if(name = document.myform.name.value)
{
S_xmlhttprequest();
xmlHttp.open("get","data.php?name="+name,true);
xmlHttp.onreadystatechange = byname;
xmlHttp.send(null);
}
}
function byname()
{
if(xmlHttp.readyState ==1)
{
document.getElementById('name').innerHTML = "<font color=red>loading....</font>";
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var name = xmlHttp.responseText;
document.getElementById('name').innerHTML = name;
}
}
}
function getEmail(email)
{
var email = document.myform.email.value;
if(email =="")
{
alert("用户名不能为空");
document.myform.email.focus();
return false;
}
else
{
S_xmlhttprequest();
xmlHttp.open("get","data.php?email="+email,true);
xmlHttp.onreadystatechange = byemail;
xmlHttp.send(null);
}
}
function byemail()
{
if(xmlHttp.readyState ==1)
{
document.getElementById('email').innerHTML = "<font color=red>loading....</font>";
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var email = xmlHttp.responseText;
document.getElementById('email').innerHTML = email;
}
}
}
2、register.php文件
3、data.php页面,用于ajax查询数据库。
由此即可实现用户注册时局部刷新的功能了。