asp中的数据提交是一个非常重要而且常用的环节,如何避免数据重复提交是个常见的问题,一般数据重复提交有2种方式:一、刷新提交后的页面,会提示重新发送信息,选择重试就会重复提交;二、按back返回再提交;所以很多人都问如何禁止back按钮的问题,这个至少目前是无法真正做到的。
我的思路是:提交数据的时候做数据合法校验,校验通过后打开一个“隐藏”的窗口(其实是显示在屏幕之外的一个小窗口)来进行提交处理,数据保存成功后刷新父窗口并用alert显示保存状态然后关闭此隐含窗口,这样用户就无法用back返回而且提交后的窗口已关闭,避免重复刷新。
下面是我的演示,只有2个文件:submitdemo.asp 和 save.asp,非常简单,只要稍微修改就可以应用到你的程序里,希望对大家有帮助。
1.submitdemo.asp 演示数据输入和校验主程序
——————————————
<html>
<head>
<title>new/edit</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript">
<!–
//打开一个位置在屏幕之外的窗口
function newhidewindow(mypage,myname)
{
leftposition = parseint(screen.width)+1;
topposition = parseint(screen.height)+1;
settings =height=100,width=100,top=+topposition+,left=+leftposition+,scrollbars=0,resizable=0,status=0
window.open(mypage,myname,settings)
}
//数据校验函数
function validate(theform)
{
if(theform.text1.value == "")
{
alert("请填写text1的数据!");
theform.text1.focus();
return (false);
}
if(theform.text2.value == "")
{
alert("请填写text2的数据!");
theform.text2.focus();
return (false);
}
return (true);
}
//调用上面两个函数校验输入的数据并打开保存数据窗口
function savewin(theform)
{
if(validate(theform))
{
newhidewindow(about:blank,savewindow);
return true;
}
return false;
}
–>
</script>
</head>
<body bgcolor="#ffffff">
<!–注意这里的 onsubmit 的函数调用和 target 中的窗口名字要和 savewin 函数中newhidewindow写的窗口名一致(注意大小写)–>
<form name="form1" action="save.asp" onsubmit="return savewin(this);" target="savewindow">
text1:
<input type="text" name="text1"><br>
text2:
<input type="text" name="text2">
<input type="submit" name="submit" value="提 交">
</form>
</body>
</html>
2.save.asp 保存数据处理
————————
<%
dim intstatus
**********************************************************
* 保存数据到数据库,在此最好再进行一次数据的合法校验 *
* … *
* if 保存成功 then *
* intstatus = 1 *
* else *
* intstatus = -1 *
* end if *
**********************************************************
成功测试
intstatus = 1
失败测试
intstatus = -1
%>
<html>
<head>
<title> </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script>
<!–
<%
if intstatus=1 then
response.write "window.opener.location.reload();"
response.write "alert(保存成功!);"
response.write "window.close();"
else
if intstatus=-1 then
response.write "alert(保存失败,请检查输入的数据是否完整有效!);window.close();"
else
response.write "window.close();"
end if
end if
%>
//–>
</script>
</head>
<body bgcolor="#ffffff">
</body>
</html>
