Php+ajax Registration Real-time verification function _php instance

Source: Internet
Author: User
Ajax real-time verification technology is very mature, on some of the standard Web site, if there is a registration verification template, it will be involved in AJAX non-refresh authentication, which is very good in the user registration experience, compared to a non-timely and background no flush docking authentication mode, to re-return to refresh, which for users, The experience is very unfriendly.

Ajax mode is almost all site application requirements, Ajax as a front-end no refresh verification, can be combined with all background language, such as Asp+ajax,php+ajax,.net+ajax and so on, the main explanation is php+ Verification of Ajax registration.

The following verification is a complete instance, we register on the site, when entering the user name, the first to do no refresh verification, to verify the background database inside the existence of the same name of the data, if there is no refresh display user name has been occupied, there are altogether four files involved.

ajax.js:The core file of Ajax technology, mainly is the verification prompt display, this file usually does not need to be modified, only need to make a front-end call.

var xmlhttpfunction 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 page from enabling caching technology 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 activexobjec T ("Microsoft.XMLHTTP"); }}return xmlHttp;} 

conn.php: This is the website configuration file, to configure your local MySQL user name and password, you need to configure the connection between PHP and the database, you can refer to the Php+mysql database connection article, in addition to deal with the data received by the actual display, If you have more than one file to validate, you can do multiple acceptance and validation here.

<?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 = "has been registered! "; } else {$response = "Congratulations! Can register! "; }  echo $response;}? >

index.html: This is the front-end file, this file is relatively simpler, called the Ajax.js processing file, will submit a non-refresh validation of the file submitted to conn.php and the background to connect.

 
   
 
  

Library. txt: This is the SQL database file, import this file into the MySQL database.

DROP DATABASE IF EXISTS ' Xin '; CREATE DATABASE ' xin '/*!40100 DEFAULT CHARACTER SET UTF8 */; Use ' Xin '; CREATE TABLE ' message ' (' id ' int (one) not NULL auto_increment, ' username ' varchar () default NULL, PRIMARY KEY (' id ')) EN Gine=innodb auto_increment=2 DEFAULT Charset=utf8;

Save these files in the root directory that can run PHP files, the test URL is localhost/index.html, the following is the package download the complete set of files, try to modify the registration module for their own needs, such as adding a password, name and so on.

SOURCE Download: Http://xiazai.php.net/201607/yuanma/php+ajaxzhuce (php.net). rar

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.

  • 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.