【问题标题】:Separate submit buttons on forms which tell form "action" to post to different files?表单上的单独提交按钮告诉表单“动作”发布到不同的文件?
【发布时间】:2011-12-19 09:35:21
【问题描述】:

我在我的表单中添加了一个额外的提交按钮,我将像这样使用它。

  1. 用户选择项目
  2. 用户点击表单上的“添加另一个项目”提交按钮
  3. 表单 POSTS 到自身并重新加载页面,以便用户可以添加其他项目
  4. 一旦用户添加了多个项目,用户点击“完成”提交按钮。
  5. 表单将所有累积的项目发布到另一个文件。

我有一种不安的感觉,这可能无法单独使用 PHP/HTML 实现,并且我可能必须使用一些 Javascript 来修改表单操作,然后才能开始 POST 数据?

想法和想法?

谢谢

【问题讨论】:

    标签: php html forms action


    【解决方案1】:

    为两个提交按钮赋予相同的名称但不同的值。您可以检查您的 php 文件中的值。

    例子

    <form action="something.php" method="post">
    <input type="submit" name="submit" value="one">
    <input type="submit" name="submit" value="two">
    </form>
    

    something.php

    switch( $_POST['submit'] ) {
        case 'one':
        case 'two':
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 JavaScript 根据点击的按钮来修改表单,或者您可以检查服务器端(即使用 PHP)点击了哪个按钮并采取相应措施。

      提交按钮是一个表单输入,就像任何其他按钮一样,即您可以给它一个名称和一个值,您可以检查服务器端。

      在客户端(即使用 JavaScript),您可以将处理程序绑定到按钮的点击事件,修改表单的操作属性并将其提交到新地址。

      这是一个客户端示例:

      <!doctype html>
      <html>
          <head>
              <title>Form submit test</title>
          </head>
          <body>
              <form action="baz.html" method="post">
                  <input id="bar" type="submit" class="button" value="bar.html" name="" />
                  <input id="foo" type="submit" class="button" value="foo.html" name="" />
              </form>
      
              <script>
                  // Find the two buttons from the DOM and assign them to separate variables
                  var barBtn = document.getElementById('bar'),
                      fooBtn = document.getElementById('foo');
      
                  // Click-handler for the buttons. 
                  // NB! For this code to work as intended, it needs to run 
                  // in the context of the button, otherwise, the this-keyword 
                  // will not resolve correctly and this will result in an error
                  // NB2! This code also requires that a button's value will be
                  // the desired action handler. Usually you would probably not
                  // do this, but use the button's name/value to lookup the 
                  // correct form action.
                  function modifyAction(e) {
                      this.form.action = this.value;
                  }
      
                  // Bind an event handler to an object
                  // NB! This is not code you should use in production
                  function bindEvent(target, event, callback) {
                      if (target.addEventListener) {
                          target.addEventListener(event, callback, false);
                      } else if (target.attachEvent) {
                          target.attachEvent('on' + event, callback);
                      }
                  }
      
                  // Delegate creates a wrapping closure which binds the 
                  // original function's context to an object, i.e. ensuring
                  // the this-keyword always refers to the same object when
                  // the returned function is invoked. 
                  function delegate(context, method) {
                      return function () {
                          return method.apply(context, arguments);
                      }
                  }
      
                  // Bind the click-event of the barBtb, and handle it
                  // with the modifyAction-function bound to the barBtn.
                  // I.e. run the modifyAction function, with the this-keyword
                  // bound to barBtn
                  bindEvent(barBtn, 'click', delegate(barBtn, modifyAction));
      
                  // Same as above for fooBtn
                  bindEvent(fooBtn, 'click', delegate(fooBtn, modifyAction));
              </script>
          </body>
      </html>
      

      为了完整起见,这里有一个相同的 jQuery 示例:

      <form action="baz.html" method="post">
          <input id="bar" type="submit" class="button" value="bar.html" name="" />
          <input id="foo" type="submit" class="button" value="foo.html" name="" />
      </form>
      
      <script>
      // Jquery event-handlers are automatically bound to
      // the element selected, so using "this" is safe
      function modifyAction(e) {
          this.form.action = this.value;
      }
      
      // Bind the click-event on all input with type=submit
      $("input[type=submit]").click(modifyAction);
      </script>
      

      【讨论】:

      • 谢谢,问题是我需要新的提交按钮来发布到两个不同的页面。如果我只有一页的 POST 表单,然后检查值。当脚本重定向到适当的页面时,我会丢失 POST 数据吗?
      • 在重定向时,您将丢失 POST 数据,是的,但是利用 PHP:s include 可以让您将代码带到您需要的地方和时间,然后您可以相应地重定向。
      • 嗨@nikc,您可以将我链接到一些信息来解释Javascript代码。我理解一开始为每个提交按钮分配两个变量的一点。但是其他的我不明白
      • @loosebruce:我可以在示例代码中添加一些解释。然而,在生产中,我相信你最好还是依赖一个跨浏览器测试的解决方案,即使用一个库,比如 jQuery、Underscore.js、Mootools 等。
      【解决方案3】:

      您可以在没有 javascript 的情况下执行此操作。只需为您的提交按钮名称提供不同的值:

      <button type="submit" name="btn" value="addItem">Add item</button>
      <button type="submit" name="btn" value="finish">Finished</button>
      

      现在在脚本中,您将表单发布到您可以通过检查 $_POST['btn'] 值来确定单击了哪个按钮并采取相应的操作。

      【讨论】:

      • 谢谢,这样我需要重定向到相应的页面。但是,这意味着如果跳转到另一个 PHP 页面,我会丢失我的 POST 数据?
      • @loosebruce,是的,如果您重定向,您将丢失 POST 数据。你不应该重定向。您只能在最后完成所有处理后重定向才能显示一些成功消息。
      • 谢谢,但我尝试制作的表格就像一个购物篮,您可以在其中选择一件事。如果您想添加更多内容,您可以单击特定的提交按钮。此按钮将导致重新加载同一页面,但将数据添加到购物篮中。这允许您添加越来越多的项目。在您单击“完成”提交按钮之前,它将转到另一个页面,您将在该页面中支付 Total 项目。对不起,如果我没有解释清楚。
      • @loosebruce,不,你的描述很清楚。只是,如果您不想使用 javascript,这是您实现它的唯一方法。
      猜你喜欢
      • 2014-02-02
      • 2021-09-13
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多