Home Backend Development PHP Tutorial Ajax real-time verification of whether username/email etc. already exists code packaging_PHP tutorial

Ajax real-time verification of whether username/email etc. already exists code packaging_PHP tutorial

Jul 21, 2016 pm 03:22 PM
ajax code share use real time Pack technology use Mail verify

Today I will share an example of "using Ajax technology to detect whether a username exists".
Principle flow chart of using Ajax technology to detect whether a user name exists:
Ajax real-time verification of whether username/email etc. already exists code packaging_PHP tutorial

Screenshot of final result:


Copy code The code is as follows:

.org/1999/xhtml">


Ajax detection username




Username:





Code explanation :
①The core code to implement this function is in ajax.js. You need to introduce
② to name the form, because later we need to use JS to get the value in the input box
③Add a " to the input box onblur" event, that is, the event is triggered when the "focus" is lost (i.e., the "trigger control" of the flow chart)
Used to put the data sent back from the server Data (i.e. "Username already exists", etc.)

Copy code The code is as follows:
mysql_connect("localhost",'root','');
mysql_select_db('test');
$sql="select * from ajax where name='$_GET[id]'";
$query=mysql_query($sql);
if(is_array(mysql_fetch_array($query))){
echo "Username already exists";
}else{
echo "Username can use ";
}
?>

Code explanation:
Through the open method of ajax, the user input "user name" is passed in through the id (i.e. $_GET[id]). At this time, the specified database table will be queried to check whether the "user name" exists
ajax.js

Copy code The code is as follows:
// JavaScript Document
var XHR; //Definition A global object
function createXHR(){ //First we have to create an XMLHttpRequest object
if(window.ActiveXObject){//Lower version of IE class
XHR=new ActiveXObject('Microsoft.XMLHTTP ');//Before IE monopolized the entire browser market and did not follow W3C standards, so this code came into being. . . But things started to change after IE6
}else if(window.XMLHttpRequest){//Non-IE series browsers, but including IE7 IE8
XHR=new XMLHttpRequest();
}
}
function checkname(){
var username=document.myform.user.value;
createXHR();
XHR.open("GET","checkname.php?id="+username ,true);//true: indicates asynchronous transmission without waiting for the send() method to return the result. This is the core idea of ​​ajax
XHR.onreadystatechange=byhongfei;//When the state changes, call the byhongfei method, We define the content of the method separately
XHR.send(null);
}
function byhongfei(){
if(XHR.readyState == 4){//About the methods in the Ajax engine object and attributes, you can refer to my other blog post: http://www.cnblogs.com/hongfei/archive/2011/11/29/2265377.html
if(XHR.status == 200){
var textHTML=XHR.responseText;
document.getElementById('checkbox').innerHTML=textHTML;
}
}
}


Code explanation:
①First we need to declare an ajax engine object: The market share of IE and other browsers is almost half each, so we have to consider both aspects, IE-->ActiveXObject; other-->XMLHttpRequest. I encapsulated her in a function: createXHR
③The checkname() function we specified in index.html will be triggered when the "focus" is lost. So how do we capture the "username" entered by the user? Here, you can easily capture document.myform.user.value using js (now you know why you named the form and input. This step corresponds to "get the filled content" of the flow chart). Interested bloggers can try it. Type the code (alert(username)) in the line before createXHR() and try popping up the captured username.
④The Ajax engine has several methods and attributes (you can refer to my other blog post: Look at the picture to understand: the difference between ordinary interaction and Ajax interaction). Before using it, we must first call the function craateXHR to create an ajax object
⑤With the ajax object, three methods are essential: open(), onreadystatechange, send().
To send a request to the server, use the open () and send() methods.
The first parameter of the open() method indicates that the transmission is to be done in GET or POST mode. . . . . .
The second parameter of the open() method indicates the URL address to be requested (here we are requesting the checkname.php file), which can be an absolute or relative address
The third parameter of the open() method async Indicates whether to use asynchronous requests, true means it is adopted. In this case, there is no need to wait for the server response through ajax and js, but: ① execute other scripts while waiting for the server response ② process the response when the response is ready. Generally, for some small requests, async=false is also acceptable, but do not write the onreadystatechange function at this time.
onreadystatechange event: This event is triggered when the ajax attribute readyState changes. In this event, when the server response is ready to be processed (that is, when readyState=4 and status=200), we specify what tasks we want the server to do. Here we specify that the results retrieved from the database will be output to id In the span tag of "checkbox".
⑥After querying the database through checkname.php, you will get the query result (that is, the server's response, corresponding to "query database" in the flow chart). At this time, the data is still in the ajax engine. If you need to obtain the query from the server To respond, we need to use the responseText or responseXML attribute of the XMLHttpRequest object, and set the data returned from the server response through the DOM attribute innerHTML to the value of the span tag with id="checkbox"
Note: There is a reason to use ajax to monitor whether the mailbox exists , we can also use ajax to monitor the password strength entered by the user in real time. At this time, we need to change the onblur event to an onfocus event.
Original cnblogs Xiaofei

Source code package download /201112/yuanma/checkname_php.rar

http://www.bkjia.com/PHPjc/324720.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324720.htmlTechArticleToday I share an example of "using Ajax technology to detect whether a user name exists". Principle flow chart of using Ajax technology to detect whether the user name exists: Screenshot of the final result: Copy the code...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

gate.io registration tutorial gate.io registration tutorial Mar 31, 2025 pm 11:09 PM

This article provides a detailed Gate.io registration tutorial, covering every step from accessing the official website to completing registration, including filling in registration information, verifying, reading user agreements, etc. The article also emphasizes security measures after successful registration, such as setting up secondary verification and completing real-name authentication, and gives tips from beginners to help users safely start their digital asset trading journey.

gate.io latest registration tutorial for beginners gate.io latest registration tutorial for beginners Mar 31, 2025 pm 11:12 PM

This article provides newbies with detailed Gate.io registration tutorials, guiding them to gradually complete the registration process, including accessing the official website, filling in information, identity verification, etc., and emphasizes the security settings after registration. In addition, the article also mentioned other exchanges such as Binance, Ouyi and Sesame Open Door. It is recommended that novices choose the right platform according to their own needs, and remind readers that digital asset investment is risky and should invest rationally.

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

The latest registration tutorial for gate.io web version The latest registration tutorial for gate.io web version Mar 31, 2025 pm 11:15 PM

This article provides a detailed Gate.io web version latest registration tutorial to help users easily get started with digital asset trading. The tutorial covers every step from accessing the official website to completing registration, and emphasizes security settings after registration. The article also briefly introduces other trading platforms such as Binance, Ouyi and Sesame Open Door. It is recommended that users choose the right platform according to their own needs and pay attention to investment risks.

Binance binance computer version entrance Binance binance computer version PC official website login entrance Binance binance computer version entrance Binance binance computer version PC official website login entrance Mar 31, 2025 pm 04:36 PM

This article provides a complete guide to login and registration on Binance PC version. First, we explained in detail the steps for logging in Binance PC version: search for "Binance Official Website" in the browser, click the login button, enter the email and password (enable 2FA to enter the verification code) to log in. Secondly, the article explains the registration process: click the "Register" button, fill in the email address, set a strong password, and verify the email address to complete the registration. Finally, the article also emphasizes account security, reminding users to pay attention to the official domain name, network environment, and regularly updating passwords to ensure account security and better use of various functions provided by Binance PC version, such as viewing market conditions, conducting transactions and managing assets.

The latest registration portal for Ouyi official website The latest registration portal for Ouyi official website Mar 21, 2025 pm 05:54 PM

As the world's leading digital asset trading platform, Ouyi OKX attracts many investors with its rich trading products, strong security guarantees and convenient user experience. However, the risks of network security are becoming increasingly severe, and how to safely register the official Ouyi OKX account is crucial. This article will provide the latest registration portal for Ouyi OKX official website, and explain in detail the steps and precautions for safe registration, including how to identify the official website, set a strong password, enable two-factor verification, etc., to help you start your digital asset investment journey safely and conveniently. Please note that there are risks in digital asset investment, please make cautious decisions.

Understand ACID properties: The pillars of a reliable database Understand ACID properties: The pillars of a reliable database Apr 08, 2025 pm 06:33 PM

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

See all articles