Multiple Selection With PHP

Category: PHP, HTML
Reviewed by: Chiggins   
Reviewed on: Oct 28 2008
» Discuss this topic ( Posts)

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" />

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.

$item = array( 'one', 'two', 'three', 'four', 'five' );