Thursday, July 3, 2008

HTML Multiple Select and PHP

Recently I've needed to create a from where certain product could belong to multiple categories and I wanted to find a best way to handle this process in limited space on the website. I was debating between multiple check boxes and a select box with "multiselect" option. Because of space and complexity of check boxes I went in direction of single multi-select dropdown. htm
Check the code below - ` was added to code to display code on blogger properly:

<`?php
foreach ($_POST['options'] as $option) {
//code to do something with each option like insert into DB
}
?>
<`html`>
<`body`>
<`form name="my_from" action="" method="post">
<`select name="options[]" multiselect> // NOTICE THAT WE ADDED [] TO INDICATE THAT THIS IS AN ARRAY!
<`option value="ABC">ABC
<`option value="123">123
<`option value="xyz">xyz
<`option value="987">987
<`/select>
<`input type="submit" name="submit_my_form" value="Submit" />
<`/form>
<`/body`>
<`/html`>