The select multiple HTML tag allows users to select multiple items from a list. These selected items are then passed to the action handler for the form with the same widget name. This could be a problem.

<select name=”var” multiple=”yes”>

Each selected option will arrive at the action handler as:

var=option1
var=option2
var=option3

Each option will overwrite the contents of the previous $var variable. The solution is to use PHP’s “array from form element” feature. The following should be used:

<select name=”var[]” multiple=”yes”>

This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.