Jump to content

Dropdown not stickying!


Tenaciousmug

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/236243-dropdown-not-stickying/
Share on other sites

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>

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>

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.