In fact, as long as the implementation of a simple Ajax detection user name, the normal point to be divided into three files. I have a simple point here:
The first one: index. PHP
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.hake.cc/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script language= "javascript" src=http://www.phpzy.com/phpjichu/"Ajax.js" ></script>
<body>
<table align= "center"
<tr>
<td width= "25%" class = "ALTBG1" > Username <font color= "Red" >*</FONT>
<input size= "name=" id = "username" type= "text" value= "onblur=" Startrequest (document.getElementById (' username '). value); "/> <br/></td>
<td></td>
<td id= "Ckuser" ></td>
</tr>
</table>
</body>
The second to use Js:ajax.js
[php ]
var xmlHttp;
function Createxmlhttprequest ()
{
if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();//mozilla browser
}
else if (window. ActiveXObject)
{
Try
{
XmlHttp = new Activex0bject ("Msxml2.xmlhttp");//ie old version
}
catch (E)
{}
Try
{
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (E)
{}
if (!xmlhttp)
{
Window.alert ("Cannot create XMLHttpRequest object instance");
return false;
}
}
}
function Startrequest (username)
{
Createxmlhttprequest ()//Special series
Xmlhttp.open ("Get", "ckuser.php?name=" +username,true);
Xmlhttp.onreadystatechange = Handlestatechange;
Xmlhttp.send (NULL);
}
function Handlestatechange ()
{
if (xmlhttp.readystate==4)
{
if (xmlhttp.status==200)
{
Alert ("Response from the server:" + xmlhttp.responsetext);
if (Xmlhttp.responsetext = = "true") {
document.getElementById ("Ckuser"). InnerHTML = ' This username is registered with the person ';
}
else if (Xmlhttp.responsetext = = "false")
{
document.getElementById ("Ckuser"). InnerHTML = ' test pass ';
}
}
}
}
[/php]
The third file is the PHP file: ckuser.php
<?php
require_once ("conn.php");
$username = $_get["name"];
$query = "SELECT id from user where username= '". $username. "';";
$res =mysql_query ($query);
if (mysql_num_ Rows ($res)!=0)
{
echo "true";
}else
{
echo "false";
}
?>
The last one is the database link file conn.php
<?php
$conn =mysql_connect ("localhost", "root", "l1314520") or Die ("Database Server connection Error". Mysql_error ());
mysql_select_db ("Test", $conn) or Die ("Database access Error". Mysql_error ());
mysql_query ("Set character set gb2312");
mysql_query ("Set names gb2312");
?>