簡體   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