您的位置:首页 > 其它

如何防止多次提交同一个表单?

2014-07-31 18:55 274 查看
http://www.cnblogs.com/jghhandsome/archive/2007/03/27/689523.html

方法(一):将button的属性改为disable

<script LANGUAGE="javascript">

function button1_onclick()

{

form1.button1.disabled=true

}

</script>

<form name=form1 >

<input type=button name=button1 onclick="button1_onclick()" value="确定">

方法(二):

在很多情况下都需要防止相同的表单被多次提交,很多人的实现方法都比较复杂(代码数量超过几十行!!)下面提供一种只需使用几行代码的方法,轻松地实现了防止用户刷新多次提交表单和使用后退钮重复多次提交表单。

表单文件formtest.asp

< %Randomize '初始代随机数种子

num1=rnd() '产生随机数num1

num1=int(26*num1)+65 '修改num1的范围以使其是A-Z范围的Ascii码,以防表单名出错

session("antry")="test"&chr(num1) '产生随机字符串

% >

< form name="test" action="testact.asp" method="post" >

你的名字:< input type='text' name='' size=30 > '注意本行中使用了随机表单项名

< input type='submit' value='提交' >

< /form >

表单处理程序testact.asp

< %

teststr=request.form(session("antry"))

if teststr="" then

response.write "没有填写姓名或重复提交"

'由于用户没有填写名字,或表单被重复提交(标志为session("antry")为空)引起

else

response.write teststr

session("antry")="" '提交成功,清空session("antry"),以防重复提交!!

end if

% >

这个不知道有没有用/

3.问:一个表单如何能有两个提交按钮?

答:用JavaScript就可以实现啊。

<html>

<head>

<script>

function submitit1()

//交由程序1处理

{

document.myForm.action = "http://www.site.com/cgi1.php"

document.myForm.submit();

}

function submitit2()

//交由程序2处理

{

document.myForm.action = "http://www.site.com/cgi2.php"

document.myForm.submit();

}

</script>

</head>

<body>

<form name="myForm" METHOD=POST>

username:<input type=text name=text1>

password:<input type=password name=text2>

<input type=button value="Submit 1" onClick="submitit1()">

<input type=button value="Submit 2" onClick="submitit2()">

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: