49

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.

Paul T.
  • 714
  • 1
  • 9
  • 20
fvgs
  • 21,412
  • 9
  • 33
  • 48

4 Answers4

104

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

powerbuoy
  • 12,460
  • 7
  • 48
  • 78
  • 2
    I was just trying this method using !empty(). Now that you mention it, isset() will probably work. EDIT: It does indeed work. Thanks for the suggestions! – fvgs Aug 13 '12 at 07:05
  • I don't remember exactly why I opted to use buttons instead of images, however I believe it was because I was unable to apply a hover to the image submit. – fvgs Aug 13 '12 at 07:14
  • Perfectly simple solution for employing multiple submit buttons. – commnux Mar 18 '17 at 23:40
  • not working in Safari – sql-noob Aug 26 '17 at 23:13
9

Give name and values to those submit buttons like:

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

and then in your php script you could check

if($_POST['mybutton'] == 'save')
{
  ///do save processing
}
elseif($_POST['mybutton'] == 'publish')
{
  ///do publish processing here
}
Dr. Dan
  • 2,288
  • 16
  • 19
  • 1
    I cannot give the buttons values because they are images. If I assign values, then that text will show up on top of my image. – fvgs Aug 13 '12 at 07:02
  • 1
    If that's the case, then you could have 2 different bussonts with different names like @powerbuoy has suggested. Then check in php script which button has been submitted. – Dr. Dan Aug 13 '12 at 07:04
  • I dont think that will work either, He said he can't put values on buttons as they are image. – WatsMyName Aug 13 '12 at 07:08
2

You can use it as follows,

<td>

<input type="submit" name="save" class="noborder" id="save" value="Save" alt="Save" 
tabindex="4" />

</td>

<td>

<input type="submit" name="publish" class="noborder" id="publish" value="Publish" 
alt="Publish" tabindex="5" />

</td>

And in PHP,

<?php
if($_POST['save'])
{
   //Save Code
}
else if($_POST['publish'])
{
   //Publish Code
}
?>
Stranger
  • 10,332
  • 18
  • 78
  • 115
2

If you can't put value on buttons. I have just a rough solution. 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.

WatsMyName
  • 4,240
  • 5
  • 42
  • 73