473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

multiple drop down menu with multiple select

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
6 7516
dlite922
1,584 Expert 1GB
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
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
1,584 Expert 1GB
point your form there. Your current form says action="", it should say action="pagewhereyouwantittogo.php"


Dan
Jul 29 '09 #4
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
1,584 Expert 1GB
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
1,584 Expert 1GB
Did you figure this out yet?



Dan
Jul 31 '09 #7

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

Similar topics

6
by: Greg Scharlemann | last post by:
I am attempting to populate a drop down menu based on the selection of a different drop down menu. However, it is not working correctly, I cannot figure out for the life of me what exactly happens...
1
by: Greg Scharlemann | last post by:
I would like to automatically populate a drop down menu when the page loads based on the selection of an item in a different drop down menu. I made a test page that when drop down #1 changes, drop...
11
by: Dan | last post by:
Hello all, I am getting records from a db and displaying the records to the user through a drop down menu in an asp page. Each record has 6 fields and I need to display them all to the user in...
3
by: Bilal | last post by:
Hi, I've been looking all over the net for a way to increase the size of a drop down menu without any success. Does anyone perhaps have a way to display 11-15 items on a menu without having...
3
by: Novice Computer User | last post by:
I have a drop down menu where people are able to select 1 item. However, I want to modify the drop down list so that the person can select more than 1 item (i.e by holding down the shift button...
4
by: mlevit | last post by:
Hey all, This is really simple but I just can't get it work. I have a drop down menu, you select your filter which will repost to the same page and grab that filter which will be used for an SQL...
2
by: phpnewbie26 | last post by:
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...
0
by: buzzard724 | last post by:
Ul li drop down menu works in FF not quite in IE Thank you for looking at this. The page is generated dynamically by php, js and jquery. The drop down menu ul- reports-li - works fine in FF. In...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.