Home >Web Front-end >JS Tutorial >jquery ajax detects whether the username exists when the user registers_jquery

jquery ajax detects whether the username exists when the user registers_jquery

WBOY
WBOYOriginal
2016-05-16 18:42:441258browse

First, you need a page to add grades, temporarily called grade.htm
This file needs to introduce two files, jquery.js (jquery framework file) and grade.js (separate file for verification).
The input below is used to enter the user name, and id="gradeInfo" is used to display prompt information.
grade.htm

Copy code The code is as follows:

*
< ;/span>

After the user enters the information, enter the verification link and see how our verification grade.js is written.
grade.js
Copy code The code is as follows:

/**
* js to verify whether the user name is repeated
*
* @name grade.js
* @author jason
* @use to verify whether the user name is duplicate Exist
* @todo
*/
$(document).ready(function(){
checkConfirm();
});
//Verify whether the username exists
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("You enter The username exists! Please re-enter!
}else{
$("#gradeInfo").html("");
}
})
return false;
})
}

I will only explain a few key points in the above js file.
1. The meaning of $("#NAME").blur is to trigger an action after the form with the id of NAME in grade.htm is entered.
2. The meaning of $(this).val() is to get the value in the form with id NAME.
3. The meaning of $.get(changeUrl, function(str) is: after running ajax, changeUrl is the address of the program to be connected, and str is the display structure obtained after the program
calculation is completed.
4. The meaning of $("#gradeInfo").html is to write the html file for the tag with id gradeInfo. The tag with id gradeInfo refers to the tag with id gradeInfo in grade.htm of
, and the displayed content will appear. Look at the location of this tag.
GradeAdmin.php
Copy code

Copy code The code is as follows:
if($frm_action == 'check')
{
$gradeName = $_GET['gradename'];
$gradeAdminObj = new Services_GradeAdmin($db);
//Determine whether the entered user name exists in the database based on $gradeName. If it exists, it returns 1. If
does not exist, it returns 0. This return value is passed Go to grade.js.
$gradeCheck = $gradeAdminObj->getGradeByName($gradeName);
if(is_numeric($gradeCheck)){
echo '1';
}else{
echo '0';
}
exit();
}

This is a problem to determine whether the user name already exists when the user registers
Statement:
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