这篇文章主要为大家详细介绍了ajax验证用户名和密码的实例代码,感兴趣的小伙伴们可以参考一下
本文实例为大家介绍了ajax验证用户名和密码的具体代码,供大家参考,具体内容如下
1.ajax主体部分
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代码
<input placeholder="用户名" autofocus="" type="text"name="username">
<input placeholder="密码" type="password" name="password">
<button id="dl" onclick="login()">登录</button>
3.这里用的是sha1加密,把你的密码和数据库名称修改成你自己的即可
<?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();
?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
织梦狗教程
本文标题为:ajax验证用户名和密码的实例代码


基础教程推荐
猜你喜欢
- 解决:layUI数据表格+简单查询 2022-12-16
- 原生ajax瀑布流demo分享(必看篇) 2023-02-01
- 关于ajax异步访问数据的问题 2023-02-23
- JavaScript垃圾回收机制(引用计数,标记清除,性能优 2022-08-31
- Unicode中的常用字母小结 2022-09-21
- AJax 把拿到的后台数据在页面中渲染的实例 2023-02-22
- 纯javascript的ajax实现php异步提交表单的简单实例 2022-12-28
- ajax实现数据分页查询 2023-01-31
- 在IE中为abbr标签加样式 2022-10-16
- Ajax提交表单并接收json实例代码 2023-02-13