Home > Article > Backend Development > php+jquery+ajax implements user name verification_PHP tutorial
This is something I accidentally saw today. It is my first time to come into contact with jquery. I write it down as an unexpected gain for me. It is just a beginner’s understanding. . .
Start writing this verification by getting to know jquery. . .
In most cases, writing jquery code requires us to place jquery code in any of the following three functions.
There are three ways to write, with the same effect, a bit like Window.onload, but there are also differences, that is, window.onload will not be executed until the page is loaded, while the $(document).ready() method only waits for all tags to be loaded. Operations, and $(document).ready() can be called multiple times, but window.onload cannot and will be overwritten by the latter call. . .
The first one: $(document).ready(function(){.....});
The second type: $().ready(function(){......});
The third type: $(function(){......});
Note: The event handler registered through the $(document).ready() method in jQuery can be called as long as the DOM is completely ready. For example, for a picture, as long as the tag is completed, there is no need to wait for the picture to be loaded. Once completed, you can set the width and height attributes or styles of the image.
Let’s see how ajax works in jquery?
ajax passes values in two ways: get/post
【get method】
var changeUrl = "CheckUser.php?uName="+name; //Jump to the judgment page
$.get(changeUrl,function(result){ //Run ajax, enter the url page, and return result
//Return value through result and output description information
} www.2cto.com
【Post method】
var username = $(this).val();
var changeUrl = "CheckUser.php";
$.post(changeUrl,{name:username},function(result){ ...}
After jumping to the CheckUser.php page, receive it through get/post and determine whether it exists from the database, and then return a value. . .
html code: Username:
jquery code: $("#userName").blur(function(){
var username = $(this).val();
var changeUrl = "CheckUser.php"; //Jump to the judgment page
//The following is to run ajax, enter the changeUrl page, pass in name, and return result
$.post(changeUrl ,{name:username},function(result){
if(result == '1'){
$("#Info").html("Username exists!");
}else{
$("#Info").html("You can register!");
} } })
CheckUser.php
if($_POST['name']){//Receive the value passed by post
$username = trim($_POST['name']);
//To make a judgment, it was originally supposed to be taken from the database. I was lazy and just tested. . .
if($username == "admin"){
echo '1';
}else{
echo '0';
}}
?>
I usually read a lot, but I just don’t know how to write about it. Maybe it’s because this time it was an accident, so I wrote it without thinking, fearing that the accident would disappear. . hehe. . .
This article is from the “Heng” blog