<!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.111cn.net
作者: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>