hankknight
asked on
PHP: 10 years in drop-down options
I want to use PHP to fill in drop-down options with the next 10 years.
<select name="ExpYear">
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
This should work:
<select name="Exp">
<?
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
$selected = '';
if(isset($_get['Exp']) && $_get['Exp'] == $y)
$selected = ' SELECTED';
echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}
}
?>
</select>
That didn't work but I did manage to use your first code an d make it work for this purpose.
Thanks!
Thanks!
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
$selected = '';
if(isset($_get['LicenseYY']) && $_get['LicenseYY'] == $y)
$selected = 'SELECTED';
echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}
// THIS WORKED
if (isset($_POST['submit'])){
echo"<option>$LicenseYY</option>";
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++)
echo '<option value="'.$y.'">'.$y.'</option>';
}else {
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++)
echo '<option value="'.$y.'">'.$y.'</option>';
}
I don't see a solution button as I am trying to award the points to you.
phpretard, the author of this question is "hankknight", only that use can close the question...
Ruurtjan wrote $_get instead of $_GET, the variable name is case sensitive. It should be done like below.
Please split the points when multiple experts have collaborated for a solution.
Ruurtjan wrote $_get instead of $_GET, the variable name is case sensitive. It should be done like below.
Please split the points when multiple experts have collaborated for a solution.
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
$selected = '';
if(isset($_GET['LicenseYY']) and ($_GET['LicenseYY'] == $y))
$selected = ' selected="selected"';
echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}
With the below format how would I return the value posted and display it as selected?
So if I select 2010 and other parts of the form did not pass validation then then 2010 would stay as the selected value.
Open in new window