How do I create arrays in a HTML <form>? <<< |
How do I get all the results from a select multiple HTML tag? | How can I pass a variable from Javascript to PHP? >>> |
10.8 PHP and HTML 10 FAQ : Foire Aux Questions Manuel PHP . What encoding/decoding do I need when I pass a value through a form/URL? . I'm trying to use an <input type="image"> tag, but the . How do I create arrays in a HTML <form>? -> How do I get all the results from a select multiple HTML tag? . How can I pass a variable from Javascript to PHP? |
10.8.4 How do I get all the results from a select multiple HTML tag?
The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. ie.
Each selected option will arrive at the action handler as:
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:
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.
Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example:
|
<< | How do I get all the results from a select multiple HTML tag? | >> |
How do I create arrays in a HTML <form>? | PHP and HTML | How can I pass a variable from Javascript to PHP? |