Hi everybody, my first post here.
I have a form in which the user selects a year from a listbox and this is used as part of a database query, the results from which are displayed on the same page. Now this works perfectly well, except that when the page reloads the lisbox selection reverts to the default.
How can I preserve the selection so that it remains selected?
The form code looks like this:
<form name="form" method="post" action="<?php echo $PHP_SELF;?>">
<select name="year">
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
I think I need some sort of switch routine that defines as selected the option chosen by the user, but I am not at all sure how to implement this.
Can anyone help me?
Thanks for looking.
preserving a listbox selection
Moderator: General Moderators
-
nowaydown1
- Forum Contributor
- Posts: 169
- Joined: Sun Apr 27, 2008 1:22 am
Re: preserving a listbox selection
Greetings. To accomplish this you want PHP be somewhat involved in the creation of your dropdown box. Have it build your <option value="2008">2008</option> tags using a loop, and as you're outputting each individual value, check the value to see if it matches what the user selected. If it does, then output the additional "selected" attribute on this <option> tag. So something along the lines of:
I haven't tested that code, but that's the basic idea. Was meant more for conceptual understanding that actual use. Hope that helps.
Code: Select all
$arrayValues = array('2008'=>'2008', '2007'=>'2007', '2006'=>'2006');
$selected = "2007";
echo "<select name=\"year\">";
if(is_array($arrayValues)) {
foreach($arrayValues as $arrayKey => $arrayValue) {
$isSelected = ($selected == $arrayKey) ? " selected" : "";
printf("<option value=\"%s\"%s>%s</option>\n", $arrayKey, $isSelected, $arrayValue);
}
}
echo "</select>";
Re: preserving a listbox selection
same as above, but just done a little different. tested='ok'
$yeararray = array('2008','2007','2006');
echo "<form name='form' method='post' action='" . $PHP_SELF . "'>";
echo "<select name='year[]'>";
for($i = 0; $i < 3; $i++){
echo "<option value='" . $yeararray[$i] . "' ";
if(isset($_POST['year'])){
if($_POST['year'][0] == $yeararray[$i]){
echo "selected";
}
}
echo ">" . $yeararray[$i] . "</option>";
}
echo "</select>";
echo "<input type='submit' name='Submit' value='Submit'>";
echo "</form>";
$yeararray = array('2008','2007','2006');
echo "<form name='form' method='post' action='" . $PHP_SELF . "'>";
echo "<select name='year[]'>";
for($i = 0; $i < 3; $i++){
echo "<option value='" . $yeararray[$i] . "' ";
if(isset($_POST['year'])){
if($_POST['year'][0] == $yeararray[$i]){
echo "selected";
}
}
echo ">" . $yeararray[$i] . "</option>";
}
echo "</select>";
echo "<input type='submit' name='Submit' value='Submit'>";
echo "</form>";
Re: preserving a listbox selection
Thank you very much, nowaydown1 and hansford. I will give hansford's code a go, and speaking as a php newbie, it looks like there's plenty to get my teeth into here.
Dougr
Dougr