0

I have a drop down box like this:

<select name="DateDropDown">
<option value="2010 selected">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
</select>

I use the value that the user selects to refresh the page and perform a query on a database. Is there a better way than the following to keep the drop down box in the correct position?

<php?
echo '<select name="DateDropDown">';
if($MyDate = '2010')
{
echo '<option value="2010 selected">2010</option>';
}
else
{
echo '<option value="2010">2010</option>';
}

... same for each entry ...

</select>
?>

?

Thanks,

David

Edited by daviddoria: n/a

3
Contributors
3
Replies
11
Views
6 Years
Discussion Span
Last Post by daviddoria
0
<select name="DateDropDown">
    <option value="2010" selected="selected">2010</option>
    <option value="2009">2009</option>
    <option value="2008">2008</option>
    </select>

im not sure if this is what you mean. :D

0

Since you are using a date drop down and using PHP will it not be easier if use a for loop? Also you can check if the $MyDate==$year option and set the selected='selected' statements. Check the code below.

<select name="DateDropDown">
<?php
	$myDate=$_GET['year'];
	for ($year=2010;$year>=2006;$year--){
		if ($myDate==$year){
			$s="selected='selected'";
		}
		else{
			$s="";
		}
		echo "<option value='$year' $s>$year</option>";	
	}
?>
</select>

Also note that in HTML what ivawafoo said is the correct syntax for the selection and in PHP you start of a PHP code with <?php not <php?.

You can test the above code by using the ?year=2006 URI.

Edited by sudeepjd: n/a

0

sudeepjd, yep - that'll do it. You're right, I had typed the code directly in Daniweb and the ? is indeed in the wrong place - just a typo.

Thanks,
David

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.