Two submit buttons in one form

2018-12-31 05:29发布

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

18条回答
余生无你
2楼-- · 2018-12-31 05:40

use formaction HTML attribute

<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>
查看更多
浅入江南
3楼-- · 2018-12-31 05:41

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:

<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
}
查看更多
其实,你不懂
4楼-- · 2018-12-31 05:41

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

查看更多
柔情千种
5楼-- · 2018-12-31 05:47

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">
查看更多
泛滥B
6楼-- · 2018-12-31 05:47

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

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

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

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

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

查看更多
伤终究还是伤i
7楼-- · 2018-12-31 05:49

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

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

查看更多
登录 后发表回答