By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,854 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,854 IT Pros & Developers. It's quick & easy.

multiple drop down menu with multiple select

P: 52
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created the problem that when I went to select the other drop down menus, the selections i made on the multiple select one would clear. Then I had tried putting the multiple select menu last so that the selections wouldn't clear but then after clicking the submit button(and going to the next page which is supposed to display the choices), it shows nothing. No selection from the multiple select menu went through. I would prefer to do it the first way and here is my code. Assume that my item[] is already created. Any help would be appreciated. Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Get the posted data, if any, or initialize as null or today's date
  3.    ($type = @$_POST['Type']) or $type = 1;
  4.    ($item = @$_POST['Item']) or $item = 1;
  5.    ($name = @$_POST['name']) or $name = "all";
  6.    ($firstdate = @$_POST['firstdate']) or $firstdate=date("ymd");
  7.    ($lastdate = @$_POST['lastdate']) or $lastdate = date("ymd");
  8.    ($userchoice = @$_POST['userchoice']) or $userchoice = "no";
  9.    ($hourchoice = @$_POST['hourchoice']) or $hourchoice = "no";
  10.    ($ratechoice = @$_POST['ratechoice']) or $ratechoice = "no";
  11.    ($amountchoice = @$_POST['amountchoice']) or $amountchoice = "no";
  12.    ($commentchoice= @$_POST['commentchoice']) or $commentchoice = "no";
  13.    $choices=array('no','yes');
  14.  
  15. // Open the form element
  16. echo '<form action="" method="post"> ';  
  17. echo '<p>User(Type username or all): <input type="text" name="name" value="', htmlentities($name),'"/><br//></p>';
  18. echo '<p>Start of Period (yymmdd): <input type="text" name="firstdate" value="', htmlentities($firstdate),'"/><br//></p>';
  19. echo '<p>End of Period(yymmdd): <input type="text" name="lastdate" value="', htmlentities($lastdate),'"/><br//></p>';
  20.  
  21. // Print first select
  22. echo '<select name="Type" onchange="submit();">';
  23. echo '<option value="">- All projects -</option>';
  24. foreach(array_keys($items) as $_type) {
  25.     // Check if this type was selected last submit
  26.     $selected = ($type == $_type ? 'selected="selected"' : '');
  27.  
  28.     // Print this type as an option
  29.     echo '<option value="'. $_type .'" '. $selected .'>'. $_type .'</option>';
  30.    }
  31. echo '</select>';
  32.  
  33. // Print the second select
  34. echo '<select name="Item[]" multiple="multiple">';
  35. echo '<option value="">- All phases -</option>';
  36. if($type) {
  37.     foreach($items[$type] as $_item) {
  38.         // Check if this item was selected last submit
  39.         $selected = ($item == $_item ? 'selected="selected"' : '');
  40.  
  41.         // Print this item as an option
  42.         echo '<option value="'. $_item .'" '. $selected .'>'. $_item .'</option>';
  43.        }
  44.    }
  45. echo '</select>';
  46. echo "<br><br>";
  47.  
  48. echo 'Display Users ';   
  49. echo '<select name="userchoice" onchange="submit();">';
  50. foreach($choices as $key=>$_selection) {
  51.        // Check if this type was selected last submit
  52.        $selected = ($userchoice == $_selection ? 'selected="selected"' : '');
  53.  
  54.        // Print this type as an option
  55.        echo '<option value="'. $_selection .'" '. $selected .'>'. $_selection .'</option>';
  56.    }
  57.    echo '</select>';
  58.    echo '<br>';
  59.    echo '<br>';
  60.  
  61. echo 'Display Hours ';   
  62. echo '<select name="hourchoice" onchange="submit();">';
  63.  
  64.    foreach($choices as $key=>$_selection) {
  65.        // Check if this type was selected last submit
  66.        $selected = ($hourchoice == $_selection ? 'selected="selected"' : '');
  67.  
  68.        // Print this type as an option
  69.        echo '<option value="'. $_selection .'" '. $selected .'>'. $_selection .'</option>';
  70.    }
  71.  echo '</select>';
  72.  echo '<br>';
  73.  echo '<br>';
  74.  echo '</form>';
  75.  
  76. echo '<form action="test.php" method="post"> ';
  77. echo'<p><input type="submit" value="Selections"/></p>';
  78. echo '</form>';
  79. ?>
  80.  
Jul 28 '09 #1
Share this Question
Share on Google+
6 Replies


dlite922
Expert 100+
P: 1,584
Here's your updated code:

Things to keep in mind:
1. Your selection button was in another form, that contained only that button, so yes, nothing would have posted even if you did print_r($_POST); which is a good way to check what's in your POST.

2. You can't compare variable $_item to array $item, you must traverse the array and compare each value or use a function that does it for you, such as in_array(), array_search(), etc.

3. Why so many echos? leave HTML as HTML, and PHP as PHP.

4. Your Welcome:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. // to test: 
  5. $projects = array('Type 1' => array('Phase 1','Phase 2','Phase 3'),'Type 2' => array('Phase 4','Phase 5'));
  6.  
  7.  
  8. // Get the posted data, if any, or initialize as null or today's date
  9. ($type = @$_POST['Type']) or $type = '';
  10. ($item = @$_POST['Item']) or $item = '';
  11. ($name = @$_POST['name']) or $name = "all";
  12. ($firstdate = @$_POST['firstdate']) or $firstdate=date("ymd");
  13. ($lastdate = @$_POST['lastdate']) or $lastdate = date("ymd");
  14. ($userchoice = @$_POST['userchoice']) or $userchoice = "no";
  15. ($hourchoice = @$_POST['hourchoice']) or $hourchoice = "no";
  16. ($ratechoice = @$_POST['ratechoice']) or $ratechoice = "no";
  17. ($amountchoice = @$_POST['amountchoice']) or $amountchoice = "no";
  18. ($commentchoice= @$_POST['commentchoice']) or $commentchoice = "no";
  19. $choices=array('no','yes');
  20.  
  21. ?>
  22.  
  23. <form action="" method="post" >
  24. <p>User(Type username or all): <input type="text" name="name" value="<?php echo $name?>"/><br/></p>
  25. <p>Start of Period (yymmdd): <input type="text" name="firstdate" value="<?php echo $firstdate?>"/><br//></p>
  26. <p>End of Period(yymmdd): <input type="text" name="lastdate" value="<?php echo $lastdate?>"/><br/></p>
  27.  
  28. <select name="Type" onchange="submit();">
  29. <option value="">- All projects -</option>
  30. <?php
  31. foreach(array_keys($projects) as $_type) {
  32.     // Check if this type was selected last submit
  33.     $selected = ($type == $_type ? 'selected="selected"' : ''); 
  34.     // Print this type as an option
  35.     echo "<option value='$_type' $selected>$_type</option>";
  36.    }
  37. ?>
  38. </select>
  39.  
  40. <select name="Item[]" multiple="multiple">
  41. <option value="">- All phases -</option>
  42. <?php
  43. if($type) {
  44.     foreach($projects[$type] as $_item) {
  45.         // Check if this item was selected last submit
  46.         $selected = (in_array($_item,$item) ? 'selected="selected"' : '');
  47.  
  48.         // Print this item as an option
  49.         echo "<option value='$_item' $selected>$_item</option>";
  50.        }
  51.    }
  52. ?>
  53. </select>
  54. <br/><br/>
  55.  
  56. Display Users
  57. <select name="userchoice" onchange="submit();">
  58. <?php
  59. foreach($choices as $key=>$_selection) {
  60.        // Check if this type was selected last submit
  61.        $selected = ($userchoice == $_selection ? 'selected="selected"' : '');
  62.  
  63.        // Print this type as an option
  64.        echo "<option value='$_selection' $selected>$_selection</option>";
  65.    }
  66. ?>
  67. </select>
  68. <br>
  69. <br>
  70.  
  71. Display Hours
  72. <select name="hourchoice" onchange="submit();">
  73. <?php 
  74.    foreach($choices as $key=>$_selection) {
  75.        // Check if this type was selected last submit
  76.        $selected = ($hourchoice == $_selection ? 'selected="selected"' : '');
  77.  
  78.        // Print this type as an option
  79.        echo "<option value='$_selection' $selected>$_selection</option>";
  80.    }
  81. ?>
  82. </select>
  83. <br>
  84. <br>
  85. <p><input type="submit" value="Selections"/></p>
  86.  
  87. </form>
  88.  
  89.  
  90.  


Dan
Jul 29 '09 #2

P: 52
Thanks!=) But I was wondering how it is supposed to get to my test.php page after I click submit?

Velora
Jul 29 '09 #3

dlite922
Expert 100+
P: 1,584
point your form there. Your current form says action="", it should say action="pagewhereyouwantittogo.php"


Dan
Jul 29 '09 #4

P: 52
I tried that before, but the problem arises as I select from any of the other drop down menus because I have onchange="submit()"; for all the drop down menus. So when I select anything, it would automatically bring me to the test.php page.

Velora
Jul 29 '09 #5

dlite922
Expert 100+
P: 1,584
ok then,

1. Learn and Use AJAX to fill your drop down

2. Use JavaScript to change the form action when you click a button.

It's never too late to learn JavaScript.



Dan
Jul 29 '09 #6

dlite922
Expert 100+
P: 1,584
Did you figure this out yet?



Dan
Jul 31 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.