Html 一种形式的两个提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/547821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:15:25  来源:igfitidea点击:

Two submit buttons in one form

htmlformssubmit

提问by Alex

I have two submitbuttons in a form. How do I determine which one was hit serverside?

我在一个表单中有两个提交按钮。如何确定哪个被击中了服务器端?

采纳答案by Greg

If you give each one a name, the clicked one will be sent through as any other input.

如果你给每个人一个名字,被点击的人将作为任何其他输入被发送。

<input type="submit" name="button_1" value="Click me">

回答by Parrots

Solution 1:
Give each input a different value and keep the same name:

解决方案 1:
给每个输入一个不同的值并保持相同的名称:

<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />

Then in the code check to see which was triggered:

然后在代码中检查以查看哪个被触发:

if ($_POST['action'] == 'Update') {
    //action for update here
} else if ($_POST['action'] == 'Delete') {
    //action for delete
} else {
    //invalid action!
}

The problem with that is you tie your logic to the user-visible text within the input.

问题在于您将逻辑与输入中的用户可见文本联系起来。



Solution 2:
Give each one a unique name and check the $_POST for the existence of that input:

解决方案 2:
给每个人一个唯一的名称并检查 $_POST 是否存在该输入:

<input type="submit" name="update_button" value="Update" />
<input type="submit" name="delete_button" value="Delete" />

And in the code:

在代码中:

if (isset($_POST['update_button'])) {
    //update action
} else if (isset($_POST['delete_button'])) {
    //delete action
} else {
    //no button pressed
}

回答by kiril

An even better solution consists of using button tags to submit the form:

一个更好的解决方案是使用按钮标签来提交表单:

<form>
    ...
    <button type="submit" name="action" value="update">Update</button>
    <button type="submit" name="action" value="delete">Delete</button>
</form>

The HTML insidethe button (e.g. ..>Update<..is what is seen by the user; because there is HTML provided, the valueis not user-visible; it is only sent to server. This way there is no inconvenience with internationalization and multiple display languages (in the former solution, the label of the button is also the value sent to the server).

按钮内部的HTML (例如..>Update<..,用户看到的;因为提供了HTML,value用户不可见;它只发送到服务器。这样就没有国际化和多种显示语言的不便(在以前的解决方案,按钮的标签也是发送到服务器的值)。

回答by Leo

There's a new HTML5 approach to this, the formactionattribute:

有一个新的 HTML5 方法,formaction属性:

<button type="submit" formaction="/action_one">First action</button>
<button type="submit" formaction="/action_two">Second action</button>

Apparently this does not work in IE9 and earlier, but for other browsers you should be fine (see: w3schools.com HTML <button> formaction Attribute).

显然这在 IE9 及更早版本中不起作用,但对于其他浏览器你应该没问题(请参阅:w3schools.com HTML <button> formaction Attribute)。

Personally, I generally use Javascript to submit forms remotely (for faster perceived feedback) with this approach as backup. Between the two, the only people not covered are IE<9 with Javascript disabled.

就个人而言,我通常使用 Javascript 以这种方法作为备份远程提交表单(以获得更快的感知反馈)。在这两者之间,唯一没有涵盖的人是禁用 Javascript 的 IE<9。

Of course, this may be inappropriate if you're basically taking the same action server-side regardless of which button was pushed, but often if there are two user-side actions available then they will map to two server-side actions as well.

当然,如果无论按下哪个按钮,您基本上都在服务器端采取相同的操作,这可能是不合适的,但通常如果有两个用户端操作可用,那么它们也会映射到两个服务器端操作。

Edit:As noted by Pascal_dher in the comments, this attribute is also available on the <input>tag as well.

编辑:正如 Pascal_dher 在评论中所指出的,该属性也可用于<input>标签。

回答by Peter Bailey

This is extremely easy to test

这非常容易测试

<form action="" method="get">

<input type="submit" name="sb" value="One">
<input type="submit" name="sb" value="Two">
<input type="submit" name="sb" value="Three">

</form>

Just put that in an HTML page, click the buttons, and look at the URL

只需将其放入 HTML 页面,单击按钮,然后查看 URL

回答by Mevlüt?zdemir

<form>
    <input type="submit" value="Submit to a" formaction="/submit/a">
    <input type="submit" value="submit to b" formaction="/submit/b">    
</form>

回答by Thirumalmani Lavanyan

Use the formactionHTML attribute (5th line):

使用formactionHTML 属性(第 5 行):

<form action="/action_page.php" method="get">
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <button type="submit">Submit</button><br>
    <button type="submit" formaction="/action_page2.php">Submit to another page</button>
</form>

回答by Shailesh Sonare

The best way to deal with multiple submit button is using switch case in server script

处理多个提交按钮的最佳方法是在服务器脚本中使用 switch case

<form action="demo_form.php" method="get">

Choose your favorite subject:

<button name="subject" type="submit" value="html">HTML</button>
<button name="subject" type="submit" value="css">CSS</button>
<button name="subject" type="submit" value="javascript">Java Script</button>
<button name="subject" type="submit" value="jquery">jQuery</button>

</form>

server code/server script - where you are submitting the form:

服务器代码/服务器脚本 - 您提交表单的位置:

demo_form.php

demo_form.php

<?php

switch($_REQUEST['subject']) {

    case 'html': //action for html here
                break;

    case 'css': //action for css here
                break;

    case 'javascript': //action for javascript here
                        break;

    case 'jquery': //action for jquery here
                    break;
}

?>

Source: W3Schools.com

资料来源:W3Schools.com

回答by Joakim

Maybe the suggested solutions here worked in 2009, but ive tested all of this upvoted answers and nobody is working in any browsers.

也许这里建议的解决方案在 2009 年有效,但我测试了所有这些赞成的答案,没有人在任何浏览器中工作。

only solution i found working is this: (but its a bit ugly to use i think)

我发现工作的唯一解决方案是:(但我认为使用起来有点难看)

<form method="post" name="form">
<input type="submit" value="dosomething" onclick="javascript: form.action='actionurl1';"/>
<input type="submit" value="dosomethingelse" onclick="javascript: form.action='actionurl2';"/>

回答by Thielicious

Define nameas array.

定义namearray

<form action='' method=POST>
    (...) some input fields (...)
    <input type=submit name=submit[save] value=Save>
    <input type=submit name=submit[delete] value=Delete>
</form>

Example server code (PHP):

示例服务器代码 (PHP):

if (isset($_POST["submit"])) {
    $sub = $_POST["submit"];

    if (isset($sub["save"])) {
        // save something;
    } elseif (isset($sub["delete"])) {
        // delete something
    }
}

elseifvery important, because both will be parsed if not. Enjoy.

elseif非常重要,因为如果没有,两者都会被解析。享受。