When registering a user, you need to know whether the user name is used. This article introduces jqueryajax to check whether the user name exists during registration. For more information about the code farmers, see. Start with a New Year's page. It is currently called grade.htm.
This file requires the introduction of two files: jquery. js (jquery framework file) and grade. js (verified individual files ).
The following input is used to enter the username. id = "gradeInfo" is used to display prompt information.
Grade.htm
*
After entering the information, the user enters the verification process to see how grade. js was written for verification.
Grade. js
/*** Verify that the user name is duplicated. js ** @ name grade. js * @ author jason
* @ Use verify whether the user name exists * @ todo */$ (document ). ready (function () {checkConfirm () ;}); // verify whether the user NAME has function checkConfirm () {$ ("# NAME "). blur (function () {var gradename = $ (this ). val (); var changeUrl = "GradeAdmin. php? Action = check & gradename = "+ gradename; $. get (changeUrl, function (str) {if (str = '1') {$ ("# gradeInfo" ).html ("the user name you entered exists! Enter again! ") ;}Else {$ (" # gradeInfo ").html (" ") ;}}) return false ;})}
In the above section of the js file, I will only explain a few key points.
1. $ ("# NAME" ).blur's meaning is that the action is triggered after the form with id NAME in grade.htm is input.
2. $ (this). val () indicates that the value in the form with id as NAME is obtained.
3. $. get (changeUrl, function (str) indicates that after running ajax, changeUrl is the address of the program to be connected, and str is the program
The display structure obtained after calculation.
4. $ ("# gradeInfo" ).html means to write html files to the tag whose id is gradeInfo. The label whose id is gradeInfo refers
In grade.htm.
Let's take a look at the calculation form of GradeAdmin. php.
GradeAdmin. php
If ($ frm_action = 'check') {$ gradeName = $ _ GET ['gradename']; $ gradeAdminObj = new Services_GradeAdmin ($ db ); // judge whether the username entered in the database is determined based on $ gradeName. If 1 exists and 0 exists, the return value is passed to grade. js. $ GradeCheck = $ gradeAdminObj-> getGradeByName ($ gradeName); if (is_numeric ($ gradeCheck) {echo '1';} else {echo '0 ';} exit ();}
This is to determine whether the user name already exists during user registration.