登录 注册

AJAX+ASP无刷新验证用户密码是否正确匹配并登录

收藏
[Javascript] 标签: 2013-10-26 09:08
原始代码 全屏查看 0评 / 0藏 / 8232阅  跳至 / 178行
对数据库操作没有太多基础经过两步最简单的验证字段是否存在操作实现验证用户名密码是否匹配
服务器端checklogin.asp 代码如下
<!--#include file="Connections/connuser.asp" --> '数据库连接代码存放在Connections/connuser.asp
<%
codepage="65001"
mail=Replace(request.querystring("email"),"'","")  '获取表单中邮箱
mima=Replace(request.querystring("password"),"'","") '获取表单中密码
sql1="select * from [user] where [email]='"&mail&"'"  '从user表中查询邮箱
sql2="select * from [user] where [email]='"&mail&"' and [password]='"&mima&"'"  '查询邮箱和密码
rs.Open sql1,conn,1,1  '数据库连接查询
if rs.eof or rs.bof then '邮箱为查到
 response.write "1"   '返回1
else                     '查到邮箱下面代码查邮箱和密码
 rs.close             '先关闭数据库连接,否则 rs.Open sql2,conn,1,1 无法建立
 rs.Open sql2,conn,1,1 
 if rs.eof or rs.bof then  '若找不到记录
 response.write "2"   '返回2
 else    '找到记录表示邮箱密码匹配
 response.write "0"  '返回0
 end if 
 end if 
 rs.close 
 %>

connuser.asp 代码:
<%
codepage="65001"
' FileName="Connection_ado_conn_string.htm"
' Type="ADO" 
' DesigntimeType="ADO"
' HTTP="true"
' Catalog=""
' Schema=""
set rs = Server.CreateObject("adodb.recordset")
set conn = Server.CreateObject("ADODB.Connection")
Dim MM_connuser_STRING
MM_connuser_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&server.Mappath("./djgjeigxigi15980/kdjgiegj128490.mdb")
conn.Open MM_connuser_STRING
%>

html代码代码中登录按钮的type属性应为button而不是submit否则页面会刷新):

<%@LANGUAGE="VBSCRIPT"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>精博图文</title>
<style type="text/css">
#top-1{ 
       
       position: fixed;
       top: 0;
       left: 0;
       padding-bottom: 1px;
       width: 100%;
       height: 40px;
  background-color:#666;
  background:-webkit-gradient(linear,left top,left bottom,from(#fff),to(#666));
}
#top-1-1{
width:960px;
height:40px;
margin:0 auto;
text-align:left;
font-size:20px;
}
#denglu{
width:600px;
    display: block;
     height: 37px;
margin:0;
     padding:3px 5px;
     line-height: 33px;
     color: #000;
     overflow: visible;}
#container{
margin:15px auto;
width:958px;
height:900px;
border:1px solid #CCC;
}
#tishi{
padding:0px;
font-size:14px; 
font-weight:600;
color:#FF0000;
}
</style>
<script type="text/javascript" language="javascript">

function checklogin(){
var mail=document.getElementByIdx_x("email").value;
    var mima=document.getElementByIdx_x("password").value;
var tishi=document.getElementByIdx_x("tishi");
var xmlhttp=false;
       //邮箱为空提示输入邮箱。
if(mail=="") { 
              tishi.innerHTML="请输入邮箱";
  denglu.style.width="625px";
 }
     //邮箱不为空,用ajax发送获取表单数据
else {
   try
   {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e)
   {
     try
     {
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch (e)
     { 
   try
   {
xmlhttp=new XMLHttpRequest();
   }
   catch (e)
   {
   }
     }
   }
    xmlhttp.open("get","checklogin.asp?email="+escape(mail)+ "&password="+escape(mima)+"&t=" +  new Date().getTime());
   xmlhttp.setRequestHeader("content-type","text/xml");
   xmlhttp.setRequestHeader("If-Modified-Since","0");
   xmlhttp.onreadystatechange=function()
   {
     if(4==xmlhttp.readyState)
{
  if(200==xmlhttp.status)
  {
         if (xmlhttp.responseText=="1")
{
tishi.innerHTML="邮箱不正确";
denglu.style.width="625px";
}
else if (xmlhttp.responseText=="2")
{
tishi.innerHTML="密码错误";
denglu.style.width="625px";
}
else
{
 tishi.innerHTML=xmlhttp.responseText;
}
  }
  else
  {
    tishi.innerHTML=xmlhttp.responseText;
denglu.style.width="630px";
  }   
}
   }
   xmlhttp.send(null);  
   return false;
   }
}
</script>
</head>
<body>
<div id="top-1" >
 <div id="top-1-1" > 
  <div id="denglu">
   <form id="f-login" name="f-login" >
   <label for="email">邮箱</label><input name="email" id="email"  type="email"  padding:0px; size="12" " /> 
   <label for="password">密码</label><input name="password" id="password" type="password"  size="12"  />
   <span id="tishi"></span>
   <input type="button" style="padding:5px 8px 5px 10px;font-size:14px; font-weight:600;cursor:pointer;" name="login" id="login" value="登录" onclick="checklogin()"/>
   <a href="#" target="_blank" style="padding:5px 8px 5px 10px;font-size:14px; font-weight:600;cursor:pointer;">注册</a>
   <a href="#" target="_blank" style="font-size:14px">找回密码</a>
   </form>
  </div></div>
</div>
<div id="container">
</div>
</body>
</html>

最新评论

  · · · · · ·  (共0条)

目前还没有评论

登录后您才可以发表评论。 马上登录 立即注册
鑫雨
2013-10-26加入
Back to Top