sawade Posted October 26, 2009 Share Posted October 26, 2009 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. Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/ Share on other sites More sharing options...
sawade Posted October 26, 2009 Author Share Posted October 26, 2009 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; } ?> Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/#findComment-944819 Share on other sites More sharing options...
sawade Posted October 27, 2009 Author Share Posted October 27, 2009 Any ideas? Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/#findComment-945429 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 The code you posted in reply#1 produces a drowdown menu from 2000-2009 when called with no parameters. How are you calling that code? Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/#findComment-945436 Share on other sites More sharing options...
dgoosens Posted October 27, 2009 Share Posted October 27, 2009 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> Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/#findComment-945444 Share on other sites More sharing options...
sawade Posted October 27, 2009 Author Share Posted October 27, 2009 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> Link to comment https://forums.phpfreaks.com/topic/179074-solved-php-generated-drop-menustickyyearxhtml-form/#findComment-945479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.