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. -
<?php
-
// Get the posted data, if any, or initialize as null or today's date
-
($type = @$_POST['Type']) or $type = 1;
-
($item = @$_POST['Item']) or $item = 1;
-
($name = @$_POST['name']) or $name = "all";
-
($firstdate = @$_POST['firstdate']) or $firstdate=date("ymd");
-
($lastdate = @$_POST['lastdate']) or $lastdate = date("ymd");
-
($userchoice = @$_POST['userchoice']) or $userchoice = "no";
-
($hourchoice = @$_POST['hourchoice']) or $hourchoice = "no";
-
($ratechoice = @$_POST['ratechoice']) or $ratechoice = "no";
-
($amountchoice = @$_POST['amountchoice']) or $amountchoice = "no";
-
($commentchoice= @$_POST['commentchoice']) or $commentchoice = "no";
-
$choices=array('no','yes');
-
-
// Open the form element
-
echo '<form action="" method="post"> ';
-
echo '<p>User(Type username or all): <input type="text" name="name" value="', htmlentities($name),'"/><br//></p>';
-
echo '<p>Start of Period (yymmdd): <input type="text" name="firstdate" value="', htmlentities($firstdate),'"/><br//></p>';
-
echo '<p>End of Period(yymmdd): <input type="text" name="lastdate" value="', htmlentities($lastdate),'"/><br//></p>';
-
-
// Print first select
-
echo '<select name="Type" onchange="submit();">';
-
echo '<option value="">- All projects -</option>';
-
foreach(array_keys($items) as $_type) {
-
// Check if this type was selected last submit
-
$selected = ($type == $_type ? 'selected="selected"' : '');
-
-
// Print this type as an option
-
echo '<option value="'. $_type .'" '. $selected .'>'. $_type .'</option>';
-
}
-
echo '</select>';
-
-
// Print the second select
-
echo '<select name="Item[]" multiple="multiple">';
-
echo '<option value="">- All phases -</option>';
-
if($type) {
-
foreach($items[$type] as $_item) {
-
// Check if this item was selected last submit
-
$selected = ($item == $_item ? 'selected="selected"' : '');
-
-
// Print this item as an option
-
echo '<option value="'. $_item .'" '. $selected .'>'. $_item .'</option>';
-
}
-
}
-
echo '</select>';
-
echo "<br><br>";
-
-
echo 'Display Users ';
-
echo '<select name="userchoice" onchange="submit();">';
-
foreach($choices as $key=>$_selection) {
-
// Check if this type was selected last submit
-
$selected = ($userchoice == $_selection ? 'selected="selected"' : '');
-
-
// Print this type as an option
-
echo '<option value="'. $_selection .'" '. $selected .'>'. $_selection .'</option>';
-
}
-
echo '</select>';
-
echo '<br>';
-
echo '<br>';
-
-
echo 'Display Hours ';
-
echo '<select name="hourchoice" onchange="submit();">';
-
-
foreach($choices as $key=>$_selection) {
-
// Check if this type was selected last submit
-
$selected = ($hourchoice == $_selection ? 'selected="selected"' : '');
-
-
// Print this type as an option
-
echo '<option value="'. $_selection .'" '. $selected .'>'. $_selection .'</option>';
-
}
-
echo '</select>';
-
echo '<br>';
-
echo '<br>';
-
echo '</form>';
-
-
echo '<form action="test.php" method="post"> ';
-
echo'<p><input type="submit" value="Selections"/></p>';
-
echo '</form>';
-
?>
-
6 7505
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: -
-
<?php
-
-
// to test:
-
$projects = array('Type 1' => array('Phase 1','Phase 2','Phase 3'),'Type 2' => array('Phase 4','Phase 5'));
-
-
-
// Get the posted data, if any, or initialize as null or today's date
-
($type = @$_POST['Type']) or $type = '';
-
($item = @$_POST['Item']) or $item = '';
-
($name = @$_POST['name']) or $name = "all";
-
($firstdate = @$_POST['firstdate']) or $firstdate=date("ymd");
-
($lastdate = @$_POST['lastdate']) or $lastdate = date("ymd");
-
($userchoice = @$_POST['userchoice']) or $userchoice = "no";
-
($hourchoice = @$_POST['hourchoice']) or $hourchoice = "no";
-
($ratechoice = @$_POST['ratechoice']) or $ratechoice = "no";
-
($amountchoice = @$_POST['amountchoice']) or $amountchoice = "no";
-
($commentchoice= @$_POST['commentchoice']) or $commentchoice = "no";
-
$choices=array('no','yes');
-
-
?>
-
-
<form action="" method="post" >
-
<p>User(Type username or all): <input type="text" name="name" value="<?php echo $name?>"/><br/></p>
-
<p>Start of Period (yymmdd): <input type="text" name="firstdate" value="<?php echo $firstdate?>"/><br//></p>
-
<p>End of Period(yymmdd): <input type="text" name="lastdate" value="<?php echo $lastdate?>"/><br/></p>
-
-
<select name="Type" onchange="submit();">
-
<option value="">- All projects -</option>
-
<?php
-
foreach(array_keys($projects) as $_type) {
-
// Check if this type was selected last submit
-
$selected = ($type == $_type ? 'selected="selected"' : '');
-
// Print this type as an option
-
echo "<option value='$_type' $selected>$_type</option>";
-
}
-
?>
-
</select>
-
-
<select name="Item[]" multiple="multiple">
-
<option value="">- All phases -</option>
-
<?php
-
if($type) {
-
foreach($projects[$type] as $_item) {
-
// Check if this item was selected last submit
-
$selected = (in_array($_item,$item) ? 'selected="selected"' : '');
-
-
// Print this item as an option
-
echo "<option value='$_item' $selected>$_item</option>";
-
}
-
}
-
?>
-
</select>
-
<br/><br/>
-
-
Display Users
-
<select name="userchoice" onchange="submit();">
-
<?php
-
foreach($choices as $key=>$_selection) {
-
// Check if this type was selected last submit
-
$selected = ($userchoice == $_selection ? 'selected="selected"' : '');
-
-
// Print this type as an option
-
echo "<option value='$_selection' $selected>$_selection</option>";
-
}
-
?>
-
</select>
-
<br>
-
<br>
-
-
Display Hours
-
<select name="hourchoice" onchange="submit();">
-
<?php
-
foreach($choices as $key=>$_selection) {
-
// Check if this type was selected last submit
-
$selected = ($hourchoice == $_selection ? 'selected="selected"' : '');
-
-
// Print this type as an option
-
echo "<option value='$_selection' $selected>$_selection</option>";
-
}
-
?>
-
</select>
-
<br>
-
<br>
-
<p><input type="submit" value="Selections"/></p>
-
-
</form>
-
-
-
Dan
Thanks!=) But I was wondering how it is supposed to get to my test.php page after I click submit?
Velora
point your form there. Your current form says action="", it should say action="pagewhereyouwantittogo.php"
Dan
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
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
Did you figure this out yet?
Dan
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |