Quote:
quote:Originally posted by nulogix
i have the following code in my listbox.html page
What size of engine would you consider?
...
however the output i get is
Engine Size(s):
and i want it to display
Engine Size(s): "whatever the user selects goes here"
how do i display multiple selections in an array through php???
|
There are a few problems here. First, you have no opening form tag.
Code:
<html>
<body>
<br />
<br />
<form action='listbox.php' method='get'>
<select name="engineSize[]" multiple>
<option>1.0L</option>
<option>1.4L</option>
<option>1.6L</option>
<option>2.0L</option>
</select>
<br />
<br />
<input type="submit" />
</form>
</body>
</html>
This is a common misconception. What you're doing is creating a multideminsional array, so the array must be formed like so $_GET['engineSize'][0].
Code:
<html>
<body>
<?php
echo "<br> Engine Size(s):";
echo $_GET['engineSize'][0];
echo $_GET['engineSize'][1];
echo $_GET['engineSize'][2];
echo $_GET['engineSize'][3];
?>
</body>
</html>
A better approach is to iterate through each indice in the array, this is done like this:
Code:
Engine Size:<br />
<?php
if (isset($_GET['engineSize']) && is_array($_GET['engineSize']) && !empty($_GET['engineSize']))
{
foreach($_GET['engineSize'] as $value)
{
echo $value."<br />\n";
}
}
else
{
echo "You did not select any engine sizes!";
}
?>
First I've checked to make sure that $_GET['engineSize'] isset, its an array, and it isn't empty, just simple validation (although I realize it isn't presented that way in the book, just FYI). Then I use a foreach loop to loop through each value contained in the $_GET['engineSize'] array, using foreach I can split the array into just its value or key, value pairs, which is tremendously useful.
This is what you use to get variables for both key and value:
foreach ($array as $key => $value)
HTH!
Regards,
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::