Home > Web Front-end > JS Tutorial > body text

Ajax implements asynchronous username verification function

韦小宝
Release: 2018-01-10 09:59:56
Original
1655 people have browsed it

When the user fills in the account and switches to the password box, use ajax to verify the availability of the account. This article mainly introduces the asynchronous user name verification function of Ajax in detail. Friends who are interested in ajax can refer to it

The layout is relatively simple first. The rendering is as follows

ajax function:

When the user fills in the account and switches to the password box, use ajax to verify the availability of the account. The verification method is as follows: first create an XMLHTTPRequest object, then send the information that needs to be verified (user name) to the server for verification, and finally determine whether the user name is available based on the status returned by the server.

function checkAccount(){
var xmlhttp;
var name = document.getElementById("account").value;
if (window.XMLHttpRequest)
 xmlhttp=new XMLHttpRequest();
else
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 
xmlhttp.open("GET","login.php?account="+name,true);
xmlhttp.send();
 
xmlhttp.onreadystatechange=function(){
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;
}
Copy after login


Running results

##Code implementation

index.html






Ajax登陆验证



使用Ajax实现异步登陆验证

账 号:

密 码:

Copy after login

login.php


<?php
  $con = mysqli_connect("localhost","root","GDHL007","sysu");
 
  if(!empty($_GET[&#39;account&#39;])){
    $sql1 = &#39;select * from login where account = "&#39;.$_GET[&#39;account&#39;].&#39;"&#39;;
    //数据库操作
    $result1 = mysqli_query($con,$sql1);
    if(mysqli_num_rows($result1)>0)
      echo &#39;<font style="color:#00FF00;">该用户存在</font>&#39;;
    else 
      echo &#39;<font style="color:#FF0000;">该用户不存在</font>&#39;;
    mysqli_close($con);
  }else
    echo &#39;<font style="color:#FF0000;">用户名不能为空</font>&#39;;
 
?>
Copy after login
The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

About the encapsulation example of ajax network request

Solution to the problem of Ajax rollback and refresh page

Ajax request and Filter cooperation case details

The above is the detailed content of Ajax implements asynchronous username verification function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!