2

I have a form with multiple submit buttons.

Each submit button is an IMG SRC trash can which denotes the delete icon for messages in a web based messaging mail inbox

what is the best way to figure out which submit button icon was clicked so that I can then write the PHP/MySQL code to DELETE the message?

if(!empty($_POST)){
        // How do I figure out which submit button has been clicked to get the ID of the message to delete?
}

<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
bobafart
  • 173
  • 2
  • 2
  • 11
  • oops sorry.. it should read – bobafart Jul 31 '13 at 04:44
  • 3
    possible duplicate of [Multiple Submit buttons on a single form](http://stackoverflow.com/questions/6129173/multiple-submit-buttons-on-a-single-form) – Brett DeWoody Jul 31 '13 at 04:45
  • 1
    possible duplicate of [Multiple submit buttons in an HTML form](http://stackoverflow.com/questions/48/multiple-submit-buttons-in-an-html-form) – Cole Tobin Jul 31 '13 at 04:48
  • dont think it's a duplicate, that link is for 2 submit buttons, I am looking for code that works for 2, 5, 10 or 1000... hence, a serialized solution – bobafart Jul 31 '13 at 04:50
  • 2
    Then just apply the same trick a thousand times ... I don't see the problem ;-) – Ja͢ck Jul 31 '13 at 04:52
  • also read the accepted answer in possible duplicate: [How can I tell which button was clicked in a PHP form submit?](https://stackoverflow.com/questions/2680160/how-can-i-tell-which-button-was-clicked-in-a-php-form-submit) there is an important side-note – papo Feb 10 '19 at 13:11
  • not a duplicate of "Multiple submit buttons in an HTML form", as that questions is asking about a layout to allow second submit in code appear as first. A different issue. – papo Feb 10 '19 at 13:14

6 Answers6

5

Set value for each submit button and check that in php and find which one is clicked

<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>

echo $_POST['submit_btn']; will give you the value of which submit button is clicked

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • ok thanks.. then how do i check to see which one was clicked? – bobafart Jul 31 '13 at 04:45
  • 2
    @bobafart You... Just no. Stop. Your name. I. Just. What? – Cole Tobin Jul 31 '13 at 04:49
  • 1
    User016, it worked. Nice job. I dont understand why it worked. I would have thought that submit_btn would always retain the last value assigned to it... but for some reason it doesn't. Not sure why. But it worked! thanks – bobafart Jul 31 '13 at 05:00
  • Yup, same question here: Why this works? What's the browser mechanism? Why doesn't it contain last value? – CoR May 13 '15 at 11:08
3

Give each button a name=""

Then you can do something like

isset($_POST['button_name']) {
      // execute code here if true
}
Becs Carter
  • 1,250
  • 1
  • 12
  • 27
  • I cant do that because there could be hundreds of buttons.. how would you account for all of them to check to see which one is clicked? – bobafart Jul 31 '13 at 04:45
3

THE solution of this problem is to use the NAME attribute of the tag input/button.

<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>

or

<button type="submit" name="submitSave">Save</button>
<button type="submit" name="submitAddComment">Add comment</button>

I think you can also use the value attribute of button tag, this is definitively not possible with input tag.

If you need to use an ID or another variable, use name="submitDelete[888]" Then, check it with PHP:

if( isset($_POST['submitDelete']) ) {
    echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
}
Loenix
  • 1,057
  • 1
  • 10
  • 23
1

So many years later, I like button because it allows to display a text or an image independently of the value returned.

Here is an illustration of possibilities which fits the title of this post and more cases than the OP.

<?php
if(!empty($_POST['id'])){
  echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
  echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
  echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
  echo 'Default clicked'; // ?
}
?>
<form method="POST">

  <!-- Original Post examples -->
  <button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
  <button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
  ...
  <button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>

  <!-- Additional possibilities -->
  <!-- ?action=create -->
  <button type="submit" name="action" value="create">New element</button>
  <!-- ?action -->
  <button type="submit" name="action">Refresh</button>
  <!-- ? -->
  <button type="submit">Default</button>
</form>
PaulH
  • 2,918
  • 2
  • 15
  • 31
0

you can give a name and a value to each of your buttons. It will then show up under $_POST['submit']

<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />
DevZer0
  • 13,433
  • 7
  • 27
  • 51
-1

You have to pass your value to the current file by declearing name and value for each.. then you can echo in your php script in order to know which one is clicked.

Vivek Kumar
  • 1,204
  • 4
  • 15
  • 35