Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use two submit buttons, and differentiate between which one was used to submit the form? [duplicate]

Currently, I have an HTML form where the user will enter a title and text for an article. When it is time to submit, they are presented with two buttons. One is to 'save' their article without publishing it, and the other is to 'publish' the article and make it public.

I'm using PHP, and I am trying to figure out how to tell which button was used, in order to store the appropriate corresponding value in the database.

<td> <input type="submit" class="noborder" id="save" value="" alt="Save" tabindex="4" /> </td> <td> <input type="submit" class="noborder" id="publish" value="" alt="Publish" tabindex="5" /> </td> 

Probably should have mentioned this earlier, but I cannot assign the buttons values because the button is an image, so the text would show up above it.

like image 552
fvgs Avatar asked Aug 13 '12 06:08

fvgs


People also ask

Can we have 2 submit buttons in a form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How can we use two submit button in one form in PHP?

Put a hidden field. And when one of the buttons are clicked before submitting, populate the value of hidden field with like say 1 when first button clicked and 2 if second one is clicked. and in submit page check for the value of this hidden field to determine which one is clicked.

What is the difference between submit and button?

A 'button' is just that, a button, to which you can add additional functionality using Javascript. A 'submit' input type has the default functionality of submitting the form it's placed in (though, of course, you can still add additional functionality using Javascript).


1 Answers

Give each input a name attribute. Only the clicked input's name attribute will be sent to the server.

<input type="submit" name="publish" value="Publish"> <input type="submit" name="save" value="Save"> 

And then

<?php     if (isset($_POST['publish'])) {         # Publish-button was clicked     }     elseif (isset($_POST['save'])) {         # Save-button was clicked     } ?> 

Edit: Changed value attributes to alt. Not sure this is the best approach for image buttons though, any particular reason you don't want to use input[type=image]?

Edit: Since this keeps getting upvotes I went ahead and changed the weird alt/value code to real submit inputs. I believe the original question asked for some sort of image buttons but there are so much better ways to achieve that nowadays instead of using input[type=image].

like image 101
powerbuoy Avatar answered Sep 28 '22 01:09

powerbuoy