Page 1 of 1

How to handle multiple submit buttons on one page?

Posted: Tue Aug 26, 2008 1:17 pm
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?

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

Posted: Tue Aug 26, 2008 1:55 pm
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.

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

Posted: Tue Aug 26, 2008 2:10 pm
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".
}
?>
 

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

Posted: Tue Aug 26, 2008 2:19 pm
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.

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

Posted: Tue Aug 26, 2008 2:20 pm
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!

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

Posted: Tue Aug 26, 2008 2:25 pm
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.

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

Posted: Tue Aug 26, 2008 3:08 pm
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>
 

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

Posted: Tue Aug 26, 2008 3:20 pm
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