前台(reg.html):
<
html
>
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB2312" />
< title > Ajax Example </ title >
< script type ="text/javascript" >
var xmlobj; //定义XMLHttpRequest对象
function CreateXMLHttpRequest()
{
if(window.XMLHttpRequest)
{//Mozilla浏览器
xmlobj=new XMLHttpRequest();
if(xmlobj.overrideMimeType)
{//设置MIME类别
xmlobj.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{//IE浏览器
try
{
xmlobj=new ActiveXObject("Msxml2.XMLHttp");
}
catch(e)
{
try
{
xmlobj=new ActiveXobject("Microsoft.XMLHttp");
}
catch(e)
{
}
}
}
}
function Validate() //主程序函数
{
CreateXMLHttpRequest(); //创建对象
var showurl = "show.php?username=" + document.getElementById ("username").value; //构造URL
xmlobj.open("GET", showurl, true); //调用validate.php
xmlobj.onreadystatechange = StatHandler; //判断URL调用的状态值并处理
xmlobj.send(null); //设置为不发送给服务器任何数据
}
function StatHandler() //用于处理状态的函数
{
if(xmlobj.readyState == 4 && xmlobj.status == 200) //如果URL成功访问,则输出网页
{
document.getElementById("msg").innerHTML=xmlobj.responseText ;
}
}
</ script >
</ head >
< body >
< p >< form action ="#" >
用户名: < input type ="text" id ="username" onBlur ="Validate();" onChange ="Validate();" >
< input type ="button" value ="用户名验证" onClick ="Validate();" >
< div id ="msg" ></ div >
</ form ></ p >
</ body >
</ html >
后台(show.php):
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB2312" />
< title > Ajax Example </ title >
< script type ="text/javascript" >
var xmlobj; //定义XMLHttpRequest对象
function CreateXMLHttpRequest()
{
if(window.XMLHttpRequest)
{//Mozilla浏览器
xmlobj=new XMLHttpRequest();
if(xmlobj.overrideMimeType)
{//设置MIME类别
xmlobj.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{//IE浏览器
try
{
xmlobj=new ActiveXObject("Msxml2.XMLHttp");
}
catch(e)
{
try
{
xmlobj=new ActiveXobject("Microsoft.XMLHttp");
}
catch(e)
{
}
}
}
}
function Validate() //主程序函数
{
CreateXMLHttpRequest(); //创建对象
var showurl = "show.php?username=" + document.getElementById ("username").value; //构造URL
xmlobj.open("GET", showurl, true); //调用validate.php
xmlobj.onreadystatechange = StatHandler; //判断URL调用的状态值并处理
xmlobj.send(null); //设置为不发送给服务器任何数据
}
function StatHandler() //用于处理状态的函数
{
if(xmlobj.readyState == 4 && xmlobj.status == 200) //如果URL成功访问,则输出网页
{
document.getElementById("msg").innerHTML=xmlobj.responseText ;
}
}
</ script >
</ head >
< body >
< p >< form action ="#" >
用户名: < input type ="text" id ="username" onBlur ="Validate();" onChange ="Validate();" >
< input type ="button" value ="用户名验证" onClick ="Validate();" >
< div id ="msg" ></ div >
</ form ></ p >
</ body >
</ html >
<?
php
$conn = mysql_connect ( " localhost " , " root " , " 123456 " );
$user = $_GET [ " username " ];
$querySQL = " SELECT * FROM users WHERE name = ' " . $user . " ' " ;
mysql_select_db ( " users " , $conn );
$rs = mysql_query ( $querySQL , $conn );
$rs_cnt = mysql_num_rows ( $rs );
if ( $rs_cnt == 1 )
{
echo " <font color=RED>该用户名已被人使用</font> " ;
}
else if ( $rs_cnt == 0 )
{
echo " <font color=GREEN>该用户名未被人使用</font> " ;
}
else
echo " <font color=RED>用户名验证程序出错</font> " ;
?>
$conn = mysql_connect ( " localhost " , " root " , " 123456 " );
$user = $_GET [ " username " ];
$querySQL = " SELECT * FROM users WHERE name = ' " . $user . " ' " ;
mysql_select_db ( " users " , $conn );
$rs = mysql_query ( $querySQL , $conn );
$rs_cnt = mysql_num_rows ( $rs );
if ( $rs_cnt == 1 )
{
echo " <font color=RED>该用户名已被人使用</font> " ;
}
else if ( $rs_cnt == 0 )
{
echo " <font color=GREEN>该用户名未被人使用</font> " ;
}
else
echo " <font color=RED>用户名验证程序出错</font> " ;
?>