Page 1 of 1

Can't get value from dynamic dropdown list

Posted: Sat May 02, 2009 10:21 am
by ,ikedodd
I'm stumped. I need to create a number of dropdown lists from a MySQL database, and then update the DB with the user's selection in each dropdown. I can successfully create the dropdowns, but can't get any values from them. Here's part of my code:

Code: Select all

 
// Lookup table of day names
$days = array(-1 => "Choose",
0 => "Sunday",
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday");
 
// TEST - Session day selection dropdown before the table.
$name= "XXX";
echo "<SELECT name=\"$name\">";
foreach ($days as $key => $value)
{
   echo "<option value=\"$value\"> $value </option>";
}
echo "</SELECT>";
 
// Elsewhere, in a loop that creates rows in the table....
$name = "YYY[]";  // Creating an array of dropdowns
echo "<td>";
echo "<SELECT NAME=\"$name\">";
foreach ($days as $key => $value)
{
   echo "<option value=\"$value\"> $value </option>";
}
echo "</SELECT>";
 
The only difference I see between these two blocks of code is the "[]" added to the SELECT name to specify an array. Later, this code gets the values from the dropdowns:

Code: Select all

 
$sessionDay = $_POST['XXX'];
$xxxKey = array_search($sessionDay, $days);
echo " xxxKey = $xxxKey<br>";  // THIS WORKS
 
$dropdownFieldArray = $_POST['YYY']; // Array of dropdown fields
$n = count($dropdownFieldArray);
echo "dropdownFieldArray has $n elements.<br>";  // This shows the correct number of dropdowns
while ($oneDropdown = each($dropdownFieldArray))
{
   $yyyKey = array_search($oneDropdown, $days);
   echo "yyyKey = $yyyKey<br>";  // THIS DISPLAYS NOTHING FOR yyyKey 
}
 
As I said, I'm completely stumped. I know the $...Array = $_POST['YYY']; and while ($one = each($...Array)) idiom and syntax are correct, because they work with regular text input fields. and clearly the $...Key = array_search($sessionDay, $days) is correct, because it works on the first (non-table) dropdown. It's just the array of dropdowns in the table that don't work.

Can anyone offer insights and suggestions to help me solve this problem? Thanks in advance.

Mike