zoukankan      html  css  js  c++  java
  • 防止重复提交

       当用户不慎操作时,容易重复提交,可能会引起数据的重新操作.通常引发重复提交有两种情况,一是按刷新键(F5或工具栏中刷新键)或是页面右键菜单中的刷新项.第二中情况是提交数据后退,再前进.   
       对于第一种情况可以在页面加个隐藏域(即隐藏框<input type=hidden>),每次提交前,给这个隐藏域赋上一个随机值,这样每次提交这个字段都是不同的.


     1<script  language="javascript" type="text/javascript">
     2    function fsubmit()
     3    {
     4        document.getElementById("hfd").value=Math.round(Math.random()*1000);
     5        form1.submit();
     6    }

     7
    </script>
     8
     9</head>
    10
    11<body>
    12<form id="form1" name="form1" method="post" action="test.aspx">
    13  <label>
    14  <input type="hidden" name="hfd"  id="hfd"/>
    15  <input type="button" name="Submit" value="提交"  onclick="fsubmit()"/>
    16  </label>
    17</form>
    18</body>


        在服务端,用Session保存这个字段的值.通常过判断Session值和传过来值比较,如果相同则提示重复提交.最后把传过来值保存在Session中,以下用于下次比较.服务端如下:


     1           bool isRepSubmit = false;
     2         if(!IsPostBack) //第一次加载时设置Session为空
     3              Session["hfd"]=null;
     4         string rand = Request.Form["hfd"];
     5         if (Session["hfd"!= null && Session["hfd"].ToString() == rand)
     6         {
     7             isRepSubmit = true;
                    //提示重复提交
     8         }
     9         else
    10             Session["hfd"= rand;

          对于第二种情况可以可以采用网页过期的办法,如下:
    在Page_Load中加入.


    1 Response.Buffer=true;
    2 Response.ExpiresAbsolute=DateTime.Now.AddSeconds(-1);
    3 Response.Expires=0;
    4 Response.CacheControl="no-cache";
  • 相关阅读:
    Apache、NGINX支持中文URL
    JS中关于clientWidth offsetWidth scrollWidth 等的含义
    设置apache登陆密码验证
    通过java代码访问远程主机
    win7
    Netty从没听过到入门 -- 服务器端详解
    分块分段
    数论-佩尔方程
    数论-毕达哥拉斯三元组
    HDU 5613-Baby Ming and Binary image
  • 原文地址:https://www.cnblogs.com/wzh13681626019/p/2844339.html
Copyright © 2011-2022 走看看