Php + ajax registration verification instance (complete code)

Source: Internet
Author: User
Tags character set http request sin create database

Html code

The code is as follows: Copy code

<Script src = "ajax. js"> </script>
<Form>
<Table>
<Tr>
<Td> user name: </td>
<Td> <input type = "text" id = "txt1" onKeyUp = "showHint (this. value)"> </td>
</Tr>
<Tr align = "center">
<Td colspan = "2"> <span id = "txtHint"> </span> </td>
</Tr>
</Table>
</Form>

Js file code

The code is as follows: Copy code

Var xmlHttp
Function showHint (str)
{
If (str. length = 0)
  {
Document. getElementById ("txtHint"). innerHTML = ""
Return
  }
XmlHttp = GetXmlHttpObject ()
If (xmlHttp = null)
  {
Alert ("Browser does not support HTTP Request ")
Return
  }
XmlHttp. onreadystatechange = stateChanged

Var geturl = "conn. php? Q = "+ str
// Sid is to add a random number to prevent the cache technology from being enabled on the page ·
Geturl = geturl + "& sid =" + Math. random ()
Geturl = encodeURI (geturl );
Geturl = encodeURI (geturl );
XmlHttp. open ("GET", geturl, true)
XmlHttp. send (null)
}
Function stateChanged ()
{
If (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete ")
 {
Document. getElementById ("txtHint"). innerHTML = xmlHttp. responseText
 }
}
Function GetXmlHttpObject ()
{
Var xmlHttp = null;
Try
 {
// Firefox, Opera 8.0 +, Safari
XmlHttp = new XMLHttpRequest ();
 }
Catch (e)
 {
// Internet Explorer
Try
  {
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
  }
Catch (e)
  {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
  }
 }
Return xmlHttp;
}

Php operations

The code is as follows: Copy code

<? Php
$ Q = $ _ GET ["q"];
$ Q = urldecode ($ q );

If (strlen ($ q)> 0)
{
$ Conn = @ mysql_connect ("localhost", "root", "1010") or die ("MySql connection error ");
Mysql_select_db ("xin", $ conn );
Mysql_query ("set names 'utf8 '");
  
$ SQL = "SELECT username FROM message WHERE username = '$ Q '";
$ Query = mysql_query ($ SQL );
@ $ Row = mysql_fetch_array ($ query );
  
If (! Empty ($ row ['username'])
  {
$ Response = "<font color = red> registered! </Font> ";
} Else
  {
$ Response = "<font color = blue> Congratulations! Yes! </Font> ";
  }
  
Echo $ response;
}

?>

Finally, the database is provided.

The code is as follows: Copy code

Drop database if exists 'sin ';
Create database 'sin '/*! 40100 default character set utf8 */;
USE 'sin ';


Create table 'message '(
'Id' int (11) not null auto_increment,
'Username' varchar (20) default NULL,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 2 default charset = utf8;

Related Article

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.