<!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=gb2312″ />
<title>prototype例子–ajax无刷新用户登录</title>
<style type=”text/css”>
body{font-size:13px;}
#msg{color:red;text-align:left;font-size:14px;}
.txt{width:120px;border:1px solid #000;}
</style>
<script src=”prototype.js” type=”text/javascript”></script>
<script type=”text/javascript”>
/*
标题:简单的prototype.js例子
功能:ajax无刷新用户登录
来自:www.aspprogram.cn
作者:wangsdong
转载请保留此信息,谢谢
*/
function chkfrom(obj)
{
$(“msg”).innerHTML=””;
var opt = {
method: ‘get’,
onSuccess: function(t)
{
m=t.responseText;
switch(m)
{
case “1”:
{
$(“msg”).innerHTML=”用户名和密码不能为空!”;
return false;
break;
}
case “2”:
{
$(“msg”).innerHTML=”错误的用户名和密码。”;
return false;
break;
}
case “3”:$(“content”).innerHTML=”恭喜你,登录成功!”;break;
}
},
on404: function(t)
{
$(“msg”).innerHTML=’错误:找不到提交页!’;
return false;
},
onFailure: function(t)
{
$(“msg”).innerHTML=’错误:’ + t.status + t.statusText;
return false;
},
asynchronous:true
}
var ajax=new Ajax.Request(‘login.asp?’+getRequestBody(obj), opt);
}
function getRequestBody(oForm)
{
var aParams = new Array();
for (var i=0 ; i < oForm.elements.length; i++) {
var sParam = encodeURIComponent(oForm.elements[i].name);
sParam += “=”;
sParam += encodeURIComponent(oForm.elements[i].value);
aParams.push(sParam);
}
return aParams.join(“&”);
}
</script>
</head>
<body>
<div id=”content”>
<form action=”#” name=”form1″ id=”form1″>
<table>
<tr>
<th colspan=”2″>登录</th>
</tr>
<tr>
<td width=”72″>用户名:</td>
<td width=”168″><input type=”text” name=”username” id=”username” class=”txt”>
</td>
</tr>
<tr>
<td>密码:</td>
<td><input type=”password” name=”password” id=”password” class=”txt”>
</td>
</tr>
<tr>
<td> </td>
<td><input type=”button” name=”submit” id=”submit” value=”登录” onclick=”javascript:chkfrom(this.form)”></td></tr>
</table>
</form>
</div>
<div id=”msg”></div>
</body>
</html>
源码下载地址:prototype例子–ajax无刷新用户登录