Home > Article > Backend Development > php ajax user registration detection code_PHP tutorial
In fact, it only requires a simple implementation of ajax to detect user names. The formal point is to divide it into three files. I’ll keep it simple here:
First one: index.php
Username* < br /> |
function createXMLHttpRequest()
{
If(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();//mozilla browser
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveX0object("Msxml2.XMLHTTP");//Old version of IE
}
catch(e)
{}
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//New version of IE
}
catch(e)
{}
if(!xmlHttp)
{
window.alert("Cannot create XMLHttpRequest object instance");
return false;
}
}
}
function startRequest(username)
{
createXMLHttpRequest();//Special Edition
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
//alert("Response from server: " + xmlHttp.responseText);
if(xmlHttp.responseText == "true"){
document.getElementById("ckuser").innerHTML = 'This username has been registered';
}
else if(xmlHttp.responseText == "false")
{
document.getElementById("ckuser").innerHTML = 'Detection passed';
}
}
}
}
The third file is the php file: ckuser.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";
}
?>
$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");
?>