3

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> 
7
  • oops sorry.. it should read <input src="...> Commented Jul 31, 2013 at 4:44
  • 3
    possible duplicate of Multiple Submit buttons on a single form Commented Jul 31, 2013 at 4:45
  • 1
    possible duplicate of Multiple submit buttons in an HTML form Commented Jul 31, 2013 at 4: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 Commented Jul 31, 2013 at 4:50
  • 2
    Then just apply the same trick a thousand times ... I don't see the problem ;-) Commented Jul 31, 2013 at 4:52

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

4 Comments

ok thanks.. then how do i check to see which one was clicked?
@bobafart You... Just no. Stop. Your name. I. Just. What?
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
Yup, same question here: Why this works? What's the browser mechanism? Why doesn't it contain last value?
4

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

Comments

3

Give each button a name=""

Then you can do something like

isset($_POST['button_name']) { // execute code here if true } 

1 Comment

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?
2

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> 

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.