Home  >  Article  >  Web Front-end  >  Example code for ajax verification of username and password

Example code for ajax verification of username and password

亚连
亚连Original
2018-05-24 09:42:411731browse

This article mainly introduces the example code of ajax verification user name and password in detail. Interested friends can refer to it

The example of this article introduces the specific code of ajax verification user name and password. , for your reference, the specific content is as follows

1.ajax main part

var xmlrequest;
function createXMLHttpRequest(){
      if(window.XMLHttpRequest){
       xmlrequest=new XMLHttpRequest();
      }
      else if(window.ActiveXObject){
         try{
           xmlrequest=new ActiveXObject("Msxm12.XMLHTTP");
         }
         catch(e){
            try{
             xmlrequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){}
         }
      
      }
}
function login(){   
 createXMLHttpRequest();
  var user = document.getElementById("yhm").value;
  var password = document.getElementById("mm").value;
  if(user==""||password==""){
   alert("请输入用户名和密码!");
   return false;
  }
  var url = "check.php?user="+user+"&password="+password;
  xmlrequest.open("POST",url,true);
  xmlrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlrequest.onreadystatechange = function(){
  if(xmlrequest.readyState == 4){
   if(xmlrequest.status==200){
      var msg = xmlrequest.responseText;   
        if(msg=='1'){
        alert('用户名或密码错误!');
        user="";
        password="";
        return false;
      } 
      else{       
        window.location.href="index1.html";
      }
    }
  }
 }
  xmlrequest.send("user="+user+"&password="+password);
 }

2.html code

  
  
  

3. Sha1 encryption is used here, just change your password and database name to your own

query($query);
$num=$result->num_rows;
if(!$num){
  echo "1";
}
 
$dp->close();
 
?>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax file upload successfully solves browser compatibility issues

laypage front-end paging plug-in implements ajax asynchronous paging

Ajax method of sending and receiving binary byte stream data

The above is the detailed content of Example code for ajax verification of username and password. For more information, please follow other related articles on the PHP Chinese website!

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