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