Quote:
quote:Originally posted by Merl
Which only returns the last selection from the list. I thick that the isset variable is part of the problem here, but simply removing it still only returns the last selection. I have tried several variation on using multiple "if..... echo...." formats and arrays and still get index errors or submit only returns the last selection.
Looking at your example Rich, I had already tried building the array in that fashion and with the current php method did not get the correct results. I sort of see where your suggested php method is going, but I don't yet understand just what you are doing there. This is my first real programing language and at this point in my learning I really need to know if any variation on the books method will work or not.
|
You are only getting the last selection on the list because you aren't appending the empty array bracket syntax on the name of the field and because of this PHP doesn't create a multi-dimensional array from the post data. multiple='multiple' only tells the browser that the user can make multiple choices. For you to truly get all of the selected choices you need to treat the field as an array on both the client-side and the server-side. By doing: name='Least[]'
<SELECT name="Least[]" multiple>
<OPTION value="Rayon">Rayon</OPTION><br>
<OPTION value="Polyester">Polyester</OPTION><br>
<OPTION value="Linen">Linen</OPTION><br>
<OPTION value="Dacron">Dacron</OPTION><br>
</SELECT>
For example if the user chooses options 1 and 4:
Rayon and Dacron:
Option 1 would be available in PHP as the following two dimensional array:
$_POST["Least"][0] == "Rayon";
And option 4 would be available in PHP as the following two dimensional array:
$_POST["Least"][1] == "Dacron";
You see what's happening? The options are numbered offset from zero and increments for each option chosen. Option 1 isn't going to be "Polyester" and Option 2 isn't going to be "Linen"... etc.
The book's example isn't that great. They ouput every possible numeric indice without checking if it first exists... this is sloppy programming.
Here's a better example:
echo "You Like Least:<br />";
foreach ($_POST["least"] as $value)
{
echo $value."<br />";
}
Now you have an example that only outputs indices present in the sub-array. foreach takes and iterates through the array till it reaches the last indice. In this example it assigns the value of each indice in the $value variable. Alternatively you can split the array into key value pairs...
foreach ($_POST["least"] as $key => $value)
$key will contain the value of the array indice, for this example it would contain 0 and 1.
An even better approach would be to test for is_array and isset first, which the book doesn't do at all:
if (isset($_POST["least"]) && is_array($_POST["least"]))
{
//foreach statement...
}
// perhaps an else statement containing an error message..
I hope that makes more sense.
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::