Multiple Selection With PHP

Chiggins - 2008-10-26 10:17:05 in PHP, HTML
Category: PHP, HTML
Reviewed by: Chiggins   
Reviewed on: Oct 28 2008
1002

Multiple selection is a very useful tool in a User Interface. It's also very handy when you need to deal with multiple objects. Say you need to clean out your Private Message inbox, which consists of 500 messages. Do you really wanna go through each and every one deleting as you go? Or would you rather delete about 70 at a time? Of course it would be much easier. Today we are going to take a general look at multiple selection with PHP.

The Code

 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Item One <input type="checkbox" name="item[]" value="one" />
    Item Two <input type="checkbox" name="item[]" value="two" />
    Item Three <input type="checkbox" name="item[]" value="three" />
    Item Four <input type="checkbox" name="item[]" value="four" />
    Item Five <input type="checkbox" name="item[]" value="five" />
</form>

The Explanation

Here is just a basic HTML form, with maybe a few snippts of code you might not have seen before.

 
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

The only weird part about this is the PHP in the action attribute. All it says is that once the form is submitted, it will return to this page.

 
Item One <input type="checkbox" name="item[]" value="one" />

Again, basic HTML with something weird in the name attribute. The brackets after the word "item" means that it will become an array. The value attribute sets a value in the array. The above code, if all "checked" would basically be the same as the following PHP.

 
<?php
$item = array( 'one', 'two', 'three', 'four', 'five' );
?>

The Code

 
<?php
if ( isset( $_POST['item'] ) ) {
    foreach ( $_POST['item'] as $item ) {
        echo "You selected item number " . $item . "<br />";
    }
}
?>

The Explanation

Here is where things get interesting. First the script checks if the global POST variable "item" has been set. If you recall from the HTML, the checkbox names were "item[]". So, $_POST['item'] is a variable array containing the checked checkbox's.

Next is a foreach loop. Basically what it does is loop through each value in the $_POST['item'] array, and assigns each value to the variable $item.

Finally, we echo a string for each value in $_POST['item'] saying which has been selected.

 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Item One <input type="checkbox" name="item[]" value="one" />
    Item Two <input type="checkbox" name="item[]" value="two" />
    Item Three <input type="checkbox" name="item[]" value="three" />
    Item Four <input type="checkbox" name="item[]" value="four" />
    Item Five <input type="checkbox" name="item[]" value="five" />
</form>
 
<?php
if ( isset( $_POST['item'] ) ) {
    foreach ( $_POST['item'] as $item ) {
        echo "You selected item number " . $item . "<br />";
    }
}
?>

Well thats basically the general way to set up a multiple selection system. Going back to our example of the Private Messages you want to delete, you can use this exact system, but instead of echoing out which PM's you want to delete, just put a SQL delete query there. This code can be expanded on to do some very cool, advanced, and useful things, so get working.