Connecting Tech Pros Worldwide Forums | Help | Site Map

multiple select drop down menu not retrieving correct data

Member
 
Join Date: Jun 2009
Posts: 52
#1: Jun 30 '09
I currently have two drop down menus where the second one is populated from the first one. My second drop down menu should be able to do multiple selection. I have searched online and found out how to do so (add in multiple and put brackets after name), but unfortunately when I retrieve the data on the next page, it seems that none of the selections were added to the variable array. On the other hand, it "POST"-ed the null value which is 1. My coding looks pretty much the same as everyone else's so I don't know why it doesn't work. Hopefully someone can shed some light. Thanks in advance. Here's my current coding.=)

Theres a lot of code before this but this is the form part.
Expand|Select|Wrap|Line Numbers
  1. // Get the posted data, if any, or initialize as null or today's date
  2.    ($type = @$_POST['Type']) or $type = 1;
  3.    ($item = @$_POST['Item']) or $item = 1;
  4.    ($name = @$_POST['name']) or $name = null;
  5.    ($firstdate = @$_POST['firstdate']) or $firstdate=date("ymd");
  6.    ($lastdate = @$_POST['lastdate']) or $lastdate = date("ymd");
  7.  
  8.  
  9.    // Open the form element
  10. echo '<form action="" method="post"> ';  
  11. echo '<p>User(Type username or all): <input type="text" name="name" value="', htmlentities($name),'"/><br//></p>';
  12. echo '<p>Start of Period (yymmdd): <input type="text" name="firstdate" value="', htmlentities($firstdate),'"/><br//></p>';
  13. echo '<p>End of Period(yymmdd): <input type="text" name="lastdate" value="', htmlentities($lastdate),'"/><br//></p>';
  14.  
  15.  // Print first select
  16.    echo '<select name="Type" onchange="submit();">';
  17.    echo '<option value="">- All projects -</option>';
  18.  
  19.    foreach(array_keys($items) as $_type) {
  20.        // Check if this type was selected last submit
  21.        $selected = ($type == $_type ? 'selected="selected"' : '');
  22.  
  23.        // Print this type as an option
  24.        echo '<option value="'. $_type .'" '. $selected .'>'. $_type .'</option>';
  25.    }
  26.    echo '</select>';
  27.  
  28.    // Print the second select
  29.    echo '<select multiple name="Item[]">';
  30.    echo '<option value="">- All phases -</option>';
  31.    if($type) {
  32.        foreach($items[$type] as $_item) {
  33.            // Check if this item was selected last submit
  34.            $selected = ($item == $_item ? 'selected="selected"' : '');
  35.  
  36.            // Print this item as an option
  37.            echo '<option value="'. $_item .'" '. $selected .'>'. $_item .'</option>';
  38.        }
  39.  
  40.    }
  41.    echo '</select>';
  42. echo "<br><br>";
  43.  
  44.  
  45.    // Close the form element
  46.    echo '</form>';
  47.  
  48.     //Do something with the selected items
  49.  $_SESSION['name'] = $name;
  50.  $_SESSION['type'] = $type;
  51.  $_SESSION['item'] = $item;
  52.  
And on the next page, I do this to check what was in $item. (Again, this is just a snippet of my code)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //opens session
  3. session_start();
  4. $items=$_SESSION['items'];
  5. print_r($item);
  6. ?>
  7.  
Thanks again.

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Denver, CO
Posts: 1,144
#2: Jun 30 '09

re: multiple select drop down menu not retrieving correct data


You're confusing the **** out of yourself with this plural and singular item/items.

At the top you call it $item, in the foreach() you call $items.

avoid doing this by calling the plural (array) version something like $all_Items.




Dan
Member
 
Join Date: Jun 2009
Posts: 52
#3: Jun 30 '09

re: multiple select drop down menu not retrieving correct data


despite the confusion between singular and plural, unfortuantely, that is not the problem. Items is the array of all my information that I collected before this form. My form works when I take out the "multiple" out of the form. So I know that the singular and plural item isnt'the problem.
Reply