/    Sign up×
Community /Pin to ProfileBookmark

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 &ge; 60</option>
<option value=”70″>DAILY MAX TEMPS &ge; 70</option>
<option value=”80″>DAILY MAX TEMPS &ge; 80</option>
<option value=”90″>DAILY MAX TEMPS &ge; 90</option>
<option value=”32″>DAILY MAX TEMPS &le; 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>[/CODE]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeMar 26.2012 — 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.
Copy linkTweet thisAlerts:
@Schmit43authorMar 26.2012 — Using the code from this thread as a model http://www.daniweb.com/web-development/php/threads/173988/retaining-selected-dropdown-box-values-after-php-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=[B]"60"[/B]>[COLOR="Magenta"]DAILY MAX TEMPS &ge; 60[/COLOR]</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 &ge; 60</option>
<option value="70">DAILY MAX TEMPS &ge; 70</option>
<option value="80">DAILY MAX TEMPS &ge; 80</option>
<option value="90">DAILY MAX TEMPS &ge; 90</option>
<option value="90">DAILY MAX TEMPS &ge; 90</option>
<option value="32">DAILY MAX TEMPS &le; 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>[/CODE]
Copy linkTweet thisAlerts:
@WolfShadeMar 26.2012 — The value is what the form submits, not the text.
Copy linkTweet thisAlerts:
@Schmit43authorMar 26.2012 — Trying to get a third drop down to work on my form. Cannot get it to post the image. I think there is something wrong with my if/else if statement

Here is the code for process.php:
[CODE]
<?php
error_reporting(E_ALL ^ E_NOTICE) ;
//If button 1 is clicked
if($_POST['item']) {
$item = $_POST['item']; }
else if ($_POST['item']){
$item = 2011;
$item = $_POST['item']; }
//if button 2 is clicked
if($_POST['item2']) {
$item2= $_POST['item2'];}
//if button 3 is clicked
if($_POST['item3']) {
$item3= $_POST['item3'];
}?>

<?php
if($_POST['Submit1']){
$url = "<img src='/WXRECORDS/images/yearly/{$_POST['item']}.png' />";
}elseif($_POST['Submit2']){
$url = "<img src='/WXRECORDS/images/frequency/{$_POST['item2']}.jpg' />";
}else($_POST['Submit3']){
$url ="<img src='/WXRECORDS/images/monthly/{$_POST['item3']}.jpg' />";}
//else{
// $url = "<img src='/WXRECORDS/images/yearly/2011.jpg' />";
//}
?>
<?php include_once('portland.html');?>
[/CODE]


Here is the code for my HTML form: Portland.html
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Portland Climate Data</title>
<link href="stylesheets/public.css" rel="stylesheet" type="text/css" >
</head>
<body style="height: 53px; color: #FFFFFF; background-color: #000000;">
<form action="process.php" method="post">
<div id="banner" class="banner">Portland Oregon Climate Data </div>
<div id="header" class="header" style="height: 35px; width:1000px" align="center">

<!DROP DOWN #1>
<select name="item" style="width: 65px">
<?php for($i=2011; $i>=1949; $i--){?>
<option value="<?php echo $i?>" <?php if($i==$_POST['item'])
echo 'selected="selected"'?>><?php echo $i?></option>
<?php }?>
</select>

<!BUTTON #1>
<input name="Submit1" type="submit" value="submit" style="height: 28px" /> &nbsp

<!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 &ge; 60</option>
<option value="70">DAILY MAX TEMPS &ge; 70</option>
<option value="80">DAILY MAX TEMPS &ge; 80</option>
<option value="90">DAILY MAX TEMPS &ge; 90</option>
<option value="90">DAILY MAX TEMPS &ge; 90</option>
<option value="32">DAILY MAX TEMPS &le; 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>

<!BUTTON #2>
<input name="Submit2" type="submit" id="submit2" value="submit" style="height: 28px" /> &nbsp

<!DROP DOWN #3>
<select name="item3" id="item3" style="width: 150px">
<?php if($_POST['submit3'] == true){ ?>
<option value="<?php echo $_POST['item3']; ?>" selected="selected"><?php echo $_POST['item3']; ?></option>
<?php }else{ ?>
<option value=""> -- select -- </option>
<?php } ?>
<option value="<?php echo $_POST['item3']; ?>" selected="selected"><?php echo $_POST['item3']; ?></option>
<option value="January-July">January-July</option>
<option value="July-December">July-December</option>

<!BUTTON #3>
<input name="Submit3" type="submit" id="submit3" value="submit" style="height: 28px" />
</div><br /></form>

<div id="content" align="center" class="content"><?php echo $url ?></div></body></html>
[/CODE]
Copy linkTweet thisAlerts:
@Schmit43authorMar 26.2012 — here is an online version of my php project:

http://www.directpaintsaleswa.com/brianschmit/WXRECORDS/process.php

Take a look at it.. You can see how the box repopulates with the field value instead of the selected name.
Copy linkTweet thisAlerts:
@WolfShadeMar 26.2012 — 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.
×

Success!

Help @Schmit43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...