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"> <button>登录</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(); ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wfyywy.html