Accessing Form Input with User-Defined Arrays

The previous example showed how to gather information from HTML elements that submit a single value per element name. This leaves us with a problem when working with SELECT elements, when it possible for the user to choose one or more items. If we name the SELECT element with a plain name, like so

<select name="products" multiple>

the script that receives this data has access to only a single value corresponding to this name. We can change this behavior by renaming an element of this kind so that its name ends with an empty set of square brackets. We do this in Listing 9.3.

Listing 9.3. An HTML Form Including a SELECT Element
 1: <html> 2: <head> 3: <title>Listing 9.3 An HTML form including a SELECT ...

Get Sams Teach Yourself PHP, MySQL® and Apache All in One now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.