PHP uses AJAX technology to detect user names

Source: Internet
Author: User

1. Build an AJAX development framework with the following code

<script language= "javascript" >var http_request = false;function createrequest (URL) {//Initialize the object and issue a XMLHttpRequest request    Http_request = false; if (window.        XMLHttpRequest) {//mozilla and other browsers http_request = new XMLHttpRequest ();        if (http_request.overridemimetype) {http_request.overridemimetype ("text/xml"); }} else if (window. ActiveXObject) {//ie browser try {http_request = new ActiveXObject ("Msxml2.xml        HTTP ");           } catch (e) {try {http_request = new ActiveXObject ("Microsoft.XMLHTTP");        } catch (E) {}}} if (!http_request) {alert ("Cannot create XMLHTTP instance!");    return false;                     } http_request.onreadystatechange = alertcontents; Specify the response method Http_request.ope                      N ("GET", url, True);           Make an HTTP request http_request.send (NULL);}  function alertcontents () {//processing the information returned by the server if (http_request.readystate = = 4)        {if (Http_request.status = =) {alert (http_request.responsetext);        } else {alert (' the page you requested found errors '); }}}</script>

2. Write the JavaScript Custom function CheckName (); To detect whether the user name is empty, when the user name is not empty, call the Createrequest () method to send the request to detect whether the user name exists, the code is as follows:

function CheckName () {    var username = form1.user.value;    if (username== "")    {        window.alert ("Enter user name");           }    else    {                                                                             createrequest (' action/checkname.php?username= ' +username+ ' &nocache= ' +new Date (). GetTime ());}    }

3. Add "Detect user name hyperlinks" on the page

<TD width= "class=" Ziti2 "><a href=" # "onclick=" CheckName () ">[Detect user name]</a></td>

4. Create a checkname.php processing page with the following code

<?phpheader ("Content-type= text/html; Charset=utf8 "); include". /conn/conn.php "; $GB 2312string=iconv (' UTF-8 ', ' Gb2312//ignore ', $RequestAjaxString);           In Ajax, first use encodeURIComponent to encode the Chinese to be submitted $username = $_get[username]; $sql =mysql_query ("select * from user where username = ' ". $username." ") Or Die (Mysql_error ()); $info =mysql_fetch_array ($sql); if ($info) {    echo "Congratulations!" user name [". $username."] Not registered! ";} else{    echo "Congratulations!" user name [". $username."] Not registered! ";}? >


Beginner PHP, in the study of the practice of recording problems, skills, code problems or have other comments welcome to make progress together ~

This article is from "Dream" blog, please be sure to keep this source http://snbo520.blog.51cto.com/3069187/1410510

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.