Tenaciousmug Posted May 12, 2011 Share Posted May 12, 2011 I'm trying to get each day, month, and year that they select stickied. If they post any errors throughout their signup, I want these values to be the same so they don't have to go through the dates again to select their birth date. <select name="dobd"> <option value="">Day</option> <?php for($i = 1; $i <= 31; $i++) { echo "<option value=\"".$i."\" selected=\"".$_POST['dobd']."\">".date('d', mktime(0,0,0,0,$i,0))."</option>\n"; } ?> </select> <select name="dobm"> <option value="">Month</option> <?php for($i = 1; $i <= 12; $i++) { echo "<option value=\"".$i."\" selected=\"".$_POST['dobm']."\">".date('M', mktime(0,0,0,$i+1,0,0))."</option>\n"; } ?> </select> <select name="doby"> <option value="">Year</option> <?php for($i = 1998; $i >= 1911; --$i) { echo "<option value=\"".$i."\" selected=\"".$_POST['doby']."\">".$i."</option>\n"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/236243-dropdown-not-stickying/ Share on other sites More sharing options...
Psycho Posted May 12, 2011 Share Posted May 12, 2011 In addition to what you requested, I made other modification to make your code flow in a more logic order. Not tested, so there may be some syntax errors. <?php //Create options for the DAY select list $dayOptions = "<option value="">Day</option>\n";; for($day = 1; $day <= 31; $day++) { $selected = ($day==$_POST['dobd']) ? ' selected="selected"' : ''; $dayOptions .= "<option value=\"{$day}\"{$selected}>".date('d', mktime(0,0,0,0,$day,0))."</option>\n"; } //Create options for the MONTH select list $monthOptions = "<option value="">Month</option>\n"; for($month = 1; $month <= 12; $month++) { $selected = ($month==$_POST['dobm']) ? ' selected="selected"' : ''; $monthOptions .= "<option value=\"{$month}\"{$selected}>".date('M', mktime(0,0,0,$month+1,0,0))."</option>\n"; } //Create options for the YEAR select list $yearOptions = "<option value="">Year</option>\n"; for($year = 1998; $year >= 1911; $year++) { $selected = ($year==$_POST['doby']) ? ' selected="selected"' : ''; $yearOptions .= "<option value=\"{$year}\"{$selected}>{$year}</option>\n"; } ?> <select name="dobd"> <?php echo $dayOptions; ?> </select> <select name="dobm"> <?php echo $monthOptions; ?> </select> <select name="doby"> <?php echo $yearOptions; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/236243-dropdown-not-stickying/#findComment-1214641 Share on other sites More sharing options...
Tenaciousmug Posted May 12, 2011 Author Share Posted May 12, 2011 You're logic works, but since they didn't post anything to begin with, it's selecting blanks and not even showing the lists... :/ <select name="dobd"> <?php $dayOptions = "<option value=\"\">Day</option>\n"; for($day = 1; $day <= 31; $day++) { $selected = ($day==$_POST['dobd']) ? ' selected="selected"' : ''; $dayOptions .= "<option value=\"".$day."\" selected=\"".$selected."\">".date('d', mktime(0,0,0,0,$day,0))."</option>\n"; } ?> </select> <select name="dobm"> <?php $monthOptions = "<option value=\"\">Month</option>\n"; for($month = 1; $month <= 12; $month++) { $selected = ($month==$_POST['dobm']) ? ' selected="selected"' : ''; $monthOptions .= "<option value=\"".$month."\" selected=\"".$selected."\">".date('M', mktime(0,0,0,$month+1,0,0))."</option>\n"; } ?> </select> <select name="doby"> <?php $yearOptions = "<option value=\"\">Month</option>\n"; for($year = 1998; $year >= 1911; --$year) { $selected = ($year==$_POST['doby']) ? ' selected="selected"' : ''; $yearOptions .= "<option value=\"".$year."\" selected=\"".$selected."\">".$year."</option>\n"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/236243-dropdown-not-stickying/#findComment-1214668 Share on other sites More sharing options...
Tenaciousmug Posted May 12, 2011 Author Share Posted May 12, 2011 Nevermind. (: Got it to work! Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/236243-dropdown-not-stickying/#findComment-1214680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.