两个提交按钮在一个表单

时间:2022-11-24 20:15:43

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

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

16 个解决方案

#1


321  

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

#2


700  

You can give each input a different value and keep the same name:

您可以给每个输入一个不同的值并保持相同的名称:

<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 only problem with that is you tie your logic to the text within the input. You could also give each one a unique name and just check the $_POST for the existence of that input:

唯一的问题是将逻辑与输入中的文本绑定。你也可以给每个人一个唯一的名字,检查$_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
}

#3


66  

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>

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).

这样就不会给国际化和多显示语言带来不便(在以前的解决方案中,按钮的标签也是发送到服务器的值)。

#4


47  

There’s a new HTML5 approach to this, the formaction attribute:

对此有一个新的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

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以这种方式作为备份,远程提交表单(以获得更快的感知反馈)。在这两者之间,唯一没有覆盖的人是IE<9,禁用了Javascript。

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在评论中指出的,这个属性也可以在标记上使用。

#5


25  

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

#6


11  

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

#7


10  

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';"/>

#8


7  

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

#9


4  

Since you didn't specify what server-side scripting method you're using, I'll give you an example that works for Python, using CherryPy (although it may be useful for other contexts, too):

由于您没有指定正在使用的服务器端脚本方法,因此我将使用CherryPy为您提供一个适用于Python的示例(尽管它可能也适用于其他上下文):

<button type="submit" name="register">Create a new account</button>
<button type="submit" name="login">Log into your account</button>

Rather than using the value to determine which button was pressed, you can use the name (with the <button> tag instead of <input>). That way, if your buttons happen to have the same text, it won't cause problems. The names of all form items, including buttons, are sent as part of the URL. In CherryPy, each of those is an argument for a method that does the server-side code. So, if your method just has **kwargs for its parameter list (instead of tediously typing out every single name of each form item) then you can check to see which button was pressed like this:

您可以使用名称(使用

if "register" in kwargs:
    pass #Do the register code
elif "login" in kwargs:
    pass #Do the login code

#10


3  

<form method="post">
<input type="hidden" name="id" value="'.$id.'" readonly="readonly"/>'; //any value to post PHP
<input type='submit' name='update' value='update' formAction='updateCars.php'/>
<input type='submit' name='delete' value='delete' formAction='sqlDelete.php'/>
</form>

#11


2  

I think you should be able to read the name/value in your GET array. I think that the button that wasn't clicked wont appear in that list.

我认为您应该能够读取GET数组中的名称/值。我认为没有点击的按钮不会出现在列表中。

#12


2  

Define name as array.

定义名称数组。

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

elseif very important, because both will be parsed if not. Enjoy.

另外,如果非常重要的话,因为如果不是的话,两者都将被解析。享受。

#13


0  

Simple you can change the action of form on different submit buttons Click.

简单来说,您可以更改表单在不同提交按钮上的操作。

Try this in document.Ready

在document.Ready试试这个

$(".acceptOffer").click(function () {
       $("form").attr("action", "/Managers/SubdomainTransactions");
});

$(".declineOffer").click(function () {
       $("form").attr("action", "/Sales/SubdomainTransactions");
});

#14


0  

You can also do it like this (I think it's very convenient if you have N inputs).

你也可以这样做(我认为如果你有N个输入,这很方便)。

<input type="submit" name="row[1]" value="something">
<input type="submit" name="row[2]" value="something">
<input type="submit" name="row[3]" value="something">

And then in the server side (PHP in my example) you can read "row" as an array to get the index:

然后在服务器端(在我的示例中是PHP),您可以将“row”作为数组读取,以获得索引:

$index = key($_POST['row']);

$_POST['row'] will be an array with just one element, in the form index => value (for example: '2' => 'something").

$_POST['row']将是一个只有一个元素的数组,其形式为index =>值(例如:'2' => 'something)。

http://php.net/manual/en/function.key.php

http://php.net/manual/en/function.key.php

#15


0  

You can also use a href attribute and send a get with the value appended for each button. But the form wouldn't be required then

您还可以使用href属性并发送带有每个按钮附加值的get。但那时不需要这种形式。

href="/SubmitForm?action=delete"
href="/SubmitForm?action=save"

#16


-1  

You can present the buttons like this:

你可以这样显示按钮:

<input type="submit" name="typeBtn" value="BUY">
<input type="submit" name="typeBtn" value="SELL">

And then in the code you can get the value using:

然后在代码中你可以使用:

if request.method == 'POST':
    #valUnits = request.POST.get('unitsInput','')
    #valPrice = request.POST.get('priceInput','')
    valType = request.POST.get('typeBtn','')

(valUnits and valPrice are some other values I extract from the form that I left in for illustration)

(valUnits和valPrice是我从我留下的表格中提取的其他一些值)

#1


321  

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

#2


700  

You can give each input a different value and keep the same name:

您可以给每个输入一个不同的值并保持相同的名称:

<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 only problem with that is you tie your logic to the text within the input. You could also give each one a unique name and just check the $_POST for the existence of that input:

唯一的问题是将逻辑与输入中的文本绑定。你也可以给每个人一个唯一的名字,检查$_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
}

#3


66  

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>

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).

这样就不会给国际化和多显示语言带来不便(在以前的解决方案中,按钮的标签也是发送到服务器的值)。

#4


47  

There’s a new HTML5 approach to this, the formaction attribute:

对此有一个新的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

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以这种方式作为备份,远程提交表单(以获得更快的感知反馈)。在这两者之间,唯一没有覆盖的人是IE<9,禁用了Javascript。

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在评论中指出的,这个属性也可以在标记上使用。

#5


25  

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

#6


11  

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

#7


10  

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';"/>

#8


7  

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

#9


4  

Since you didn't specify what server-side scripting method you're using, I'll give you an example that works for Python, using CherryPy (although it may be useful for other contexts, too):

由于您没有指定正在使用的服务器端脚本方法,因此我将使用CherryPy为您提供一个适用于Python的示例(尽管它可能也适用于其他上下文):

<button type="submit" name="register">Create a new account</button>
<button type="submit" name="login">Log into your account</button>

Rather than using the value to determine which button was pressed, you can use the name (with the <button> tag instead of <input>). That way, if your buttons happen to have the same text, it won't cause problems. The names of all form items, including buttons, are sent as part of the URL. In CherryPy, each of those is an argument for a method that does the server-side code. So, if your method just has **kwargs for its parameter list (instead of tediously typing out every single name of each form item) then you can check to see which button was pressed like this:

您可以使用名称(使用

if "register" in kwargs:
    pass #Do the register code
elif "login" in kwargs:
    pass #Do the login code

#10


3  

<form method="post">
<input type="hidden" name="id" value="'.$id.'" readonly="readonly"/>'; //any value to post PHP
<input type='submit' name='update' value='update' formAction='updateCars.php'/>
<input type='submit' name='delete' value='delete' formAction='sqlDelete.php'/>
</form>

#11


2  

I think you should be able to read the name/value in your GET array. I think that the button that wasn't clicked wont appear in that list.

我认为您应该能够读取GET数组中的名称/值。我认为没有点击的按钮不会出现在列表中。

#12


2  

Define name as array.

定义名称数组。

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

elseif very important, because both will be parsed if not. Enjoy.

另外,如果非常重要的话,因为如果不是的话,两者都将被解析。享受。

#13


0  

Simple you can change the action of form on different submit buttons Click.

简单来说,您可以更改表单在不同提交按钮上的操作。

Try this in document.Ready

在document.Ready试试这个

$(".acceptOffer").click(function () {
       $("form").attr("action", "/Managers/SubdomainTransactions");
});

$(".declineOffer").click(function () {
       $("form").attr("action", "/Sales/SubdomainTransactions");
});

#14


0  

You can also do it like this (I think it's very convenient if you have N inputs).

你也可以这样做(我认为如果你有N个输入,这很方便)。

<input type="submit" name="row[1]" value="something">
<input type="submit" name="row[2]" value="something">
<input type="submit" name="row[3]" value="something">

And then in the server side (PHP in my example) you can read "row" as an array to get the index:

然后在服务器端(在我的示例中是PHP),您可以将“row”作为数组读取,以获得索引:

$index = key($_POST['row']);

$_POST['row'] will be an array with just one element, in the form index => value (for example: '2' => 'something").

$_POST['row']将是一个只有一个元素的数组,其形式为index =>值(例如:'2' => 'something)。

http://php.net/manual/en/function.key.php

http://php.net/manual/en/function.key.php

#15


0  

You can also use a href attribute and send a get with the value appended for each button. But the form wouldn't be required then

您还可以使用href属性并发送带有每个按钮附加值的get。但那时不需要这种形式。

href="/SubmitForm?action=delete"
href="/SubmitForm?action=save"

#16


-1  

You can present the buttons like this:

你可以这样显示按钮:

<input type="submit" name="typeBtn" value="BUY">
<input type="submit" name="typeBtn" value="SELL">

And then in the code you can get the value using:

然后在代码中你可以使用:

if request.method == 'POST':
    #valUnits = request.POST.get('unitsInput','')
    #valPrice = request.POST.get('priceInput','')
    valType = request.POST.get('typeBtn','')

(valUnits and valPrice are some other values I extract from the form that I left in for illustration)

(valUnits和valPrice是我从我留下的表格中提取的其他一些值)