Jump to content

[SOLVED] PHP Generated Drop menu/Sticky/Year/XHTML Form


sawade

Recommended Posts

I am trying to transfer my HTML drop down menu into a php generated menu.

 

<select name="dobyr" id="dobyr" class="dropdown">
<option value="">Year</option>
<option value="1900" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1900') { echo 'selected="selected"'; } ?> >1900</option>
<option value="1901" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1901') { echo 'selected="selected"'; } ?> >1901</option>
<option value="1902" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1902') { echo 'selected="selected"'; } ?> >1902</option>
<option value="1903" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1903') { echo 'selected="selected"'; } ?> >1903</option>
<option value="1904" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1904') { echo 'selected="selected"'; } ?> >1904</option>
<option value="1905" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1905') { echo 'selected="selected"'; } ?> >1905</option>
<option value="1906" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1906') { echo 'selected="selected"'; } ?> >1906</option>
<option value="1907" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1907') { echo 'selected="selected"'; } ?> >1907</option>
<option value="1908" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1908') { echo 'selected="selected"'; } ?> >1908</option>
<option value="1909" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1909') { echo 'selected="selected"'; } ?> >1909</option>
<option value="1910" <?php if(isset($_POST['dobyr']) && $_POST['dobyr'] == '1910') { echo 'selected="selected"'; } ?> >1910</option>
..etc
</select>

 

I need it to generate starting from 1900 to the current year.  As well as automatically update so when we move into 2010 I don't need to make any changes to the code.  I have found some examples on the net, but none of them seem to be helping me.  I need the user selection to remain ie "sticky".  And pass the value into a preset variable $dob_year

 

Thanks for the help.

I tried to use this for a trial.  But it doesn't show up... just white space.

 

<?php function create_year_dropdown($start_year = FALSE, $end_year = FALSE) {
// Setup start and end years
$start_year = ($start_year) ? $start_year - 1 : date('Y') - 10;
$end_year = ($end_year) ? $end_year : date('Y');

// Generate the menu
$return_value = '<select name="dobyr">';
for ($i = $end_year; $i > $start_year; $i -= 1) {
$return_value .= '<option value="'.$i.'">'.$i.'</option>';
}
$return_value .= '</select>';

return $return_value;
} ?>

somthing like this

 

<select name="dobyr" id="dobyr" class="dropdown">
<option value="">Year</option>
<?php
$thisYear = date('Y');
for($i=1900; $i<=$thisYear; $i++) {
$str = "<option value=\"$i\"";
if( $_POST['dobyr'] && $_POST['dobyr'] == $i) {
   $str .= " selected=\"selected\"";
}
$str .= ">";
$str .= $i;
$str .= "</option>";

echo $str;
}
?>
</select>

This works awesome!  Thanks a lot!

 

You are saving me tons a time.  :)

 

somthing like this

 

<select name="dobyr" id="dobyr" class="dropdown">
<option value="">Year</option>
<?php
$thisYear = date('Y');
for($i=1900; $i<=$thisYear; $i++) {
$str = "<option value=\"$i\"";
if( $_POST['dobyr'] && $_POST['dobyr'] == $i) {
   $str .= " selected=\"selected\"";
}
$str .= ">";
$str .= $i;
$str .= "</option>";

echo $str;
}
?>
</select>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.