Implementation Code for AJAX user uniqueness Verification

Source: Internet
Author: User

Verify from the username User table in database my:
Checkusername.html:
The Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>
<Script language = "javascript">
Var xmlHttp;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}
Function send_request (url, data ){
CreateXMLHttpRequest ();
XmlHttp. open ("POST", url, true );
XmlHttp. onreadystatechange = check_lll;
XmlHttp. setRequestHeader ("CONTENT-TYPE", "application/x-www-form-urlencoded ");
XmlHttp. send ("username =" + data );
}
Function check_lll (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Alert (xmlHttp. responseText );
}
}
}
Function check_username (){
Var f = document. form1;
Var username = f. username. value;
If (username = ""){
Alert ("NULL ");
Return false;
}
Else {
Send_request ("check_it.php", username );
}
}
</Script>
<Body>
<Form id = "form1" name = "form1" method = "post" action = "">
<P> </p> <p>
Name: <input type = "text" name = "username"/>
</P>
<Input type = "button" value = "check it" onclick = "check_username ()"/>
<P> </p>
<P> </p>
</Form>
</Body>
</Html>

Check_it.php:
The Code is as follows:
<? Php
$ Username = $ _ POST ["username"];
$ Conn = mysql_connect ("localhost: 3306", "root", "123 ");
Mysql_select_db ("my", $ conn );
$ SQL = "select * from username where username = '$ username '";
$ Result = mysql_query ($ SQL, $ conn );
$ Num = mysql_fetch_array ($ result );
If ($ num> 0 ){
Printf ("can't use ");
}
Else {
Printf ("It can use ");
}

?>

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.