首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >避免通过Javascript多次提交表单

避免通过Javascript多次提交表单
EN

Stack Overflow用户
提问于 2014-02-26 04:26:26
回答 3查看 157关注 0票数 0

让我明确标题的含义:在我的代码中,为了验证一个依赖于字段"t1“的字段,我需要自动提交表单一次(只有一次)。但是我下面的代码提交了无数次,我知道为什么会发生这种情况。我猜原因是每次表单提交时,标题中的JS都会运行。请帮我避免这种情况。以下是我的代码:

代码语言:javascript
运行
AI代码解释
复制
<html>
<head>
<script>
window.onload = function()
{   
    var f = document.getElementById("CheckForm");
    var temp = document.getElementById("CheckForm.t1");
    if(f.name == "CheckForm")
    {
      var temp1 = document.getElementById("t1");
      temp1.value = "Task";
    }
      document.CheckForm.submit();
}
</script>
</head>
<body>
      <form name="CheckForm" id="CheckForm" method="Post">
    <input type="text" id="t1" name="t1"/>
</form>
</body>
</html>

我尝试使用像标志这样的变量和像arguments.callee.count = ++arguments.callee.count || 1这样的静态变量来阻止它,并将我的CheckForm.submit()行放在if子句中。但都不管用。任何建议或帮助都是值得欣赏的。

EN

回答 3

Stack Overflow用户

发布于 2014-02-26 04:34:57

代码语言:javascript
运行
AI代码解释
复制
<html>
<head>
<script>
window.onload = function()
{   
    var f = document.getElementById("t1");
    var temp = document.getElementById("CheckForm.t1");
    if(f.name == "CheckForm")
    {
      var temp1 = document.getElementById("CheckForm.t1");
      temp1.value = "Task";
    }
      if(window.location.search=="")document.CheckForm.submit();
}
</script>
</head>
<body>
      <form name="CheckForm">
              <input type="text" id="t1"/>
      </form>
</body>
</html>
票数 1
EN

Stack Overflow用户

发布于 2014-02-26 04:38:05

您的表单肯定比以下内容更复杂:

代码语言:javascript
运行
AI代码解释
复制
  <form name="CheckForm">
          <input type="text" id="t1">
  </form>

这将不会向服务器提交任何内容,因为没有成功的控件(唯一的控件没有名称)。

由于表单只是提交到同一页,因此可以提交一个隐藏值,如下所示:

代码语言:javascript
运行
AI代码解释
复制
  <form name="CheckForm">
          <input type="text" id="t1">
          <input type="hidden" name="hasBeenSubmitted" value="yes">
  </form>

现在,当表单提交时,新页面的URL将包含...?hasBeenSubmitted=yes,这样您就可以在URL中查找该值,例如

代码语言:javascript
运行
AI代码解释
复制
if (/hasBeenSubmitted=yes/.test(window.location.search)) {
    // this page loaded because the form was submitted
}

如果存在,请不要再次提交该表单。

票数 1
EN

Stack Overflow用户

发布于 2014-03-02 20:30:45

因此,既然你使用的是post方法,最简单的方法就是提交一个新的url,然而,你似乎决定将表单提交给同一个url,在这种情况下,你使用的是php (或任何其他语言),你可以检查http请求是否有一个值为t1的post属性。

代码语言:javascript
运行
AI代码解释
复制
<?php 

if(isset($_POST['t1']){
        $your_text=$_POST['t1'];
        /*do some string checking to make safe and the throw into your database or mdo whatever you please with data
        if you wanted to include the ip address of the user you can get a basic and most likely client ip address like so

        $ip_address= $_SERVER['REMOTE_ADDR'];

        if you are handing a mulitpage form look into php session or similar tech ... cookies is kind of over kill for this scenario

         then include a succes page as the form has been submitted
         or you could end php with this tag ?> and then have your html and start again with <?
        */

        include 'form_submitted.php';

}else{
    //this would be the html page that you included in your question and can be handle in same ways as form submitted 
    include 'my_form.php'
}
?>

Ip地址可能不是最好的,因为它会阻止两个用户填写表格,如果他们在同一个LAN中,例如。两个人在同一个办公室或同一个房子里(如果你的页面在万维网上是有效的)。我会看看@RobG answer,因为他基本上是在用get而不是post ANyways来建议相同类型的事情,希望这会有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22031735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档