Php+ajax when checking user name or mail registration there are instances tutorial _php tips

Source: Internet
Author: User

Php+ajax detection of user name or mail registration is already existing in the forum or membership system is a common important function. This paper describes the implementation method of this function in the form of an instance. The specific steps are as follows:

First, the PHP test page

The check.php page code is as follows:

<script type= "Text/javascript" src= "jiance.js" ></script> <form name= "MyForm" action= "method="
Get >
 username: <input name= "user" value= "type=" text "onblur=" funtest100 () "/> <div id="
 test100 "> </div>
</form>

Second, the AJAX verification page

The Check.js page code is as follows:

var xmlHttp;
function S_xmlhttprequest () {
  if (window). ActiveXObject) {
    xmlHttp = new ActiveXObject (' microsoft.xmlhttp ');
  } else if (window. XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest ();
  }
}
function funtest100 () {
  var f = document.getelementsbytagname_r (' form ') [0].user.value;//gets text box content
  S_ XMLHttpRequest ();
  Xmlhttp.open ("Get", "jcfor.php?id=" +f,true);/Open Request
  Xmlhttp.onreadystatechange = byphp;//ready to execute
  Xmlhttp.send (null);//Send
}
function byphp () {
  //Judge status
  if (xmlhttp.readystate==1) {//ajax State
    document.getelementbyidx_x_x (' test100 '). InnerHTML = "Loading";
  }
  if (xmlhttp.readystate==4) {//ajax state
    if (xmlhttp.status==200) {//server-side State
      var bytest100 = Xmlhttp.responsetext;
      alert (bytest100);
      document.getelementbyidx_x_x (' test100 '). InnerHTML = bytest100;}}


Third, PHP verification page

The chkfor.php page code is as follows:

<?php
 if ($_get[id]) {sleep
    (1);
    $conn =mysql_connect (' localhost ', ' root ', ');
    mysql_select_db (' Test ', $conn);
    $sql = "SELECT * from ' user ' WHERE ' name ' = ' $_get[id] '";
    $q =mysql_query ($sql);
 
    if (Is_array (Mysql_fetch_row ($q)) {
      echo "user name already exists"; 
    } else{
      echo "User name can be used" 
    ;
 }  
? >

I hope the example described in this article will help you with PHP program development.

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.