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
<input placeholder="用户名" autofocus="" type="text"name="username"> <input placeholder="密码" type="password" name="password"> <button id="dl" onclick="login()">登录</button>
3. Sha1 encryption is used here, just change your password and database name to your own
<?php $yhm1=$_POST['user']; $mm1=$_POST['password']; @ $dp=new mysqli('localhost','root','你的密码','你的数据库名称'); $yhm2=sha1($yhm1); $mm2=sha1($mm1); $query="select * from zhuce where yhm='$yhm2' and mm='$mm2'"; $result=$dp->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!