Home  >  Article  >  Backend Development  >  php ajax user registration detection code_PHP tutorial

php ajax user registration detection code_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:51833browse

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




Untitled Document





                                                                                                                                                                                                                                                                                                                                                                                  

Username*
 ​ < br />



The second one requires js: ajax.js
[php]

var xmlHttp;

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.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 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';
}
}
}
}

[/php]

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";
                                                                                                          }

?>

The last one is the database link file conn.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");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371914.htmlTechArticleIn fact, it only needs to simply implement ajax to detect the user name. The formal point is to divide it into three files. Let me keep it simple here: the first one: index. php !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//E...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn