PHP Ajax User Registration detection code

Source: Internet
Author: User

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");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.