繁体   English   中英

表单上的单独提交按钮告诉表单“操作”发布到不同的文件?

[英]Separate submit buttons on forms which tell form “action” to post to different files?

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

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

我有一种不安的感觉,即仅靠PHP / HTML可能无法实现这一点,并且我可能必须在表单开始发布数据之前使用一些Javascript来修改表单操作?

有什么想法和想法?

谢谢

为两个提交按钮指定相同的名称,但使用不同的值。 您可以检查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':
}

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

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

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

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

提交按钮是一个表单输入,就像其他任何输入一样,即,您可以为其指定名称和值,并可以在服务器端进行检查。

在客户端(即使用JavaScript),您可以将处理程序绑定到按钮的click-event,修改表单的action-attribute并将其提交到新地址。

这是一个客户端示例:

<!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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM