Retaining Drop Down List Selection after a form submit
I have this drop down list. I am trying to capture the selected value of the drop down and have it remain selected after a submit. I have tried to work with this but I never got the syntax correct. Any ideas?
Code:
<!DROP DOWN #2>
<select name="item2" style="width: 250px">
<option value="60">DAILY MAX TEMPS ≥ 60</option>
<option value="70">DAILY MAX TEMPS ≥ 70</option>
<option value="80">DAILY MAX TEMPS ≥ 80</option>
<option value="90">DAILY MAX TEMPS ≥ 90</option>
<option value="32">DAILY MAX TEMPS ≤ 32</option>
<option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
<option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
<option value="raindays">DAY OF RAINFALL PER MONTH</option>
<option value="spring_occ">FIRST SPRING OCCURENCES</option>
<option value="rainfall">MONTHLY/YEARLY RAINFALL</option>
<!The below code is supposed to retain the selected drop down but does not>
<option value="<?php $_POST['item2']
echo 'selected="selected"'?>><?php echo ['item2']?></option>
First, you need to insert an option at the very beginning of all the options in the select, give it a value of blank (ie, ""), and text of "Select" or something like that.
Next, param the value of that select to be blank (again, "") if it doesn't already exist. (That way, if it does exist - as it should after submit - it won't be over-written.)
Thirdly, each option within the select needs to have an if/then condition added to it that checks the value of the submitted against the value of the option - if it matches, echo or print " selected='true'" inside the option tag for that value.
Using the code from this thread as a model http://www.daniweb.com/web-developme...hp-form-submit
I came up with the following code that works.. woo hoo.. thank you very much.
I have one more question however. The value retained in the drop down after selection is the value submitted to the $_POST array. I was hoping to be able to populate the drop down with the named value instead.
For instance:
I get the value in bold below returned. What I want is the magenta text in the box. Any ideas? I am so close here.
<option value="60">DAILY MAX TEMPS ≥ 60</option>
Code:
<!DROP DOWN #2>
<select name="item2" id="item2" style="width: 250px">
<?php if($_POST['submit2'] == true){ ?>
<option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
<?php }else{ ?>
<option value=""> -- select -- </option>
<?php } ?>
<option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
<option value="60">DAILY MAX TEMPS ≥ 60</option>
<option value="70">DAILY MAX TEMPS ≥ 70</option>
<option value="80">DAILY MAX TEMPS ≥ 80</option>
<option value="90">DAILY MAX TEMPS ≥ 90</option>
<option value="90">DAILY MAX TEMPS ≥ 90</option>
<option value="32">DAILY MAX TEMPS ≤ 32</option>
<option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
<option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
<option value="raindays">DAY OF RAINFALL PER MONTH</option>
<option value="spring_occ">FIRST SPRING OCCURENCES</option>
<option value="rainfall">MONTHLY/YEARLY RAINFALL</option>
A select box (unlike a radio or checkbox) exists, even if there is nothing selected. Instead of checking true/false, check value. If it has a length of 0, it wasn't populated with a value.
Bookmarks