How to handle multiple submit buttons on one page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lutrova
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 1:06 pm

How to handle multiple submit buttons on one page?

Post by Lutrova »

I have a small problem. I've been coding this website for some time now and I've hit a snag. I'm just coding a quick and dirty administrator's interface. Basically, people submit information that is publicly browseable and only after admin/mod approval. On the admin interface, I have each entry laid out for me as it would generally appear to the visitor so I can review each one on the page. There are two buttons per listing labeled "Accept" and "Reject". Their functions should be pretty obvious. Let's say I have 3 listings waiting approval, so that means 6 buttons total. The question is, if I click one button for one listing, how do I make the script differentiate between the buttons? How do I make it know that one set of Accept/Reject buttons belongs to Listing ID #5 or whatever so that when I click Reject for that one the script is supposed to know which listing to take action on?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to handle multiple submit buttons on one page?

Post by jayshields »

There are a few ways to go about this. Personally I use quite a dirty way. Name the buttons as arrays (so, f.e. you'll name all the reject buttons "reject[]") then, with every record you're allowing the buttons to be pressed, output a hidden input field with the name set to something like "id[]", and set the value to be the record's ID.

In your form handling code you can do something like if(isset($_POST['reject'])) to see if one of the reject buttons has been pressed, then you can do something like array_search('Reject', $_POST['reject']) to grab the index of the one which was pressed. Finally, you can find the ID of the record to be rejected in $_POST['id'] with the index returned from the array_search() call.
Lutrova
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 1:06 pm

Re: How to handle multiple submit buttons on one page?

Post by Lutrova »

jayshields wrote:There are a few ways to go about this.
You actually gave me an idea. I started experimenting and came up with a slightly more clean method. I have the following for each record:

Code: Select all

 
<form action="button_test.php" method="post">
    <b>14:</b>
    <button name="accept" value="14">Accept</button>
    <button name="reject" value="14">Reject</button>
</form>
 
Then in PHP I do this:

Code: Select all

 
<?
if (!empty($_POST)) {
    $key = key($_POST);
    $value = current($_POST);
    // Do appropriate action to the current() record depending on if the $key is "accept" or "reject".
}
?>
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to handle multiple submit buttons on one page?

Post by jayshields »

That's not valid HTML, and if you changed it to be valid then the buttons would have a number printed on them instead of "Reject" or "Accept". An alternative method to what I suggested previously (if you're using GET, or don't mind using it instead of POST), is to use buttons with an onclick attribute to reload the page with an appropriate query string.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: How to handle multiple submit buttons on one page?

Post by dajawu »

I have the exact same setup on my site, where I have to approve/reject a bunch or stuff at once. Personally I use an array of check boxes, or you can use radio buttons as well. That way you have one submit button, and your code just loops through all the boxes/radios that are selected. Nice and sweet!
Lutrova
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 1:06 pm

Re: How to handle multiple submit buttons on one page?

Post by Lutrova »

jayshields wrote:That's not valid HTML, and if you changed it to be valid then the buttons would have a number printed on them instead of "Reject" or "Accept". An alternative method to what I suggested previously (if you're using GET, or don't mind using it instead of POST), is to use buttons with an onclick attribute to reload the page with an appropriate query string.
Not valid HTML? W3C still has it listed as a valid and usable tag and it seems to operate exactly as one would expect in Firefox 3 and IE7 from my experiments. Whatever the case, this is only for admin use and isn't going to be visible to users. This is more of a dirty thrown-together job for my use as it is.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: How to handle multiple submit buttons on one page?

Post by Ziq »

I have the exact same setup on my site, where I have to approve/reject a bunch or stuff at once. Personally I use an array of check boxes, or you can use radio buttons as well. That way you have one submit button, and your code just loops through all the boxes/radios that are selected. Nice and sweet!
It's perfect resolution.

But if you need to distinguish two submit button, use this simple way

Code: Select all

 
<?
if (!empty($_POST['id']))
{
    if (isset($_POST['accept']))
    {
        //  Accept manipulation
        echo $id.' accepted';
    }
    else if (isset($_POST['reject']))
    {
        //  Reject manipulation
        echo $id.' rejected';
    }
}
?>
<form method="POST">
    Any text
    <input type="hidden" name="id" value="14">
    <input type="submit" name="accept" value="Accept this!">
    <input type="submit" name="reject" value="Reject this!">
</form>
 
Lutrova
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 1:06 pm

Re: How to handle multiple submit buttons on one page?

Post by Lutrova »

Ziq wrote:
I have the exact same setup on my site, where I have to approve/reject a bunch or stuff at once. Personally I use an array of check boxes, or you can use radio buttons as well. That way you have one submit button, and your code just loops through all the boxes/radios that are selected. Nice and sweet!
It's perfect resolution.

But if you need to distinguish two submit button, use this simple way

Code: Select all

 
<?
if (!empty($_POST['id']))
{
    if (isset($_POST['accept']))
    {
        //  Accept manipulation
        echo $id.' accepted';
    }
    else if (isset($_POST['reject']))
    {
        //  Reject manipulation
        echo $id.' rejected';
    }
}
?>
 
Thank you very much for the tip! :D
Post Reply