Php + ajax register real-time verification function _ php instance

Source: Internet
Author: User
When registering on the website, we must first perform a refreshing verification when entering the user name. This article mainly introduces the real-time registration verification function for php + ajax, interested partners can refer to ajax's real-time verification technology, which is very mature. if there are registration verification templates on some standard websites, it will certainly involve no-refreshing ajax verification, which is great in the user registration experience. compare a verification mode that cannot be promptly connected to the backend without refreshing the code, and refresh it by returning the code, this is unfriendly to users.

The ajax mode is almost the requirement of all site applications. as a front-end without refreshing verification, ajax can be used in combination with all background languages, suchAsp + ajax, php + ajax,. net + ajaxAnd so on. here we mainly explain the registration and verification of php + ajax.

The following verification is a complete example. when we register on the website, when entering the user name, we must first perform a non-refreshing verification to verify whether there is data with the same name in the background database, if the user name already exists, the prompt that the user name is occupied is not displayed. four files are involved.

Ajax. js:The core file of ajax technology is the display of verification prompts from time to time. This File usually does not need to be modified and only needs to be called by the front-end.

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

Conn. php:This is the configuration file of the website. to configure your local mysql User name and password, you must configure the connection between PHP and the database. for details, refer to the article on connection between PHP and MYSQL databases, in addition, to process the received data for real-time display, if there are multiple files to be verified, here you can perform multiple acceptance and verification.

<? 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 = "already registered! ";} Else {$ response =" Congratulations! Yes! ";}Echo $ response ;}?>

Index.html:This is the front-end file, which is relatively simpler and calls ajax. the js processing file will be submitted to the conn. connect php to the background.

 

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.