Jump to content

creating dynamic dropdown boxes for date


chiefrokka

Recommended Posts

if i have a dropdown box for the Day, Month, Year...... how do I make the default dropdown option "selected" depending on todays day/month? 

 

On any given day, I want the Day and Month dropdown boxes to start with todays date and then of course list all the other options

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/91398-creating-dynamic-dropdown-boxes-for-date/
Share on other sites

This isn't all of it.  I figured I would just get you started and allow you to figure out the rest.

 

	<select size="1" name="day"><?

//write out all the options for 1-31 days
for ($i = 0; $i < 32; $i++)
{
	echo "\n\t <option ";
	if (date("j") == $i)
		echo "SELECTED ";
	echo "value\"=".$i."\">".$i."</option>";
}?>
</select>  
<select size="1" name="year">
<?
for ($jj = (date("Y") - 2); $jj < (date("Y") + 15); $jj++)
{
	echo "\n\t <option ";
	if (date("Y") == $jj)
		echo "SELECTED ";
	echo "value\"=".$jj."\">".$jj."</option>";
}
?>
</select>  

 

Summary...

 

Allows the user to select the day of the month and it will select todays date.  In this case it would show all options but the 16th would be selected.

 

Allows the user to select the year beginning from 2 years ago to 15 years from now.  In this case it would show choices for 2006 - 2023 but 2008 would actually be the one selected.

 

It will NOT decipher the differences between if there are 28,29,30 or 31 days in the month.  You might have to do some java/ajax stuff with that if you want a live dynamic menu.  For example if the user selected feb for the month then you would have to somehow check and/or allow them to not choose 29,30 or 31 for the day of the month.  In addition in leap years you would allow them to choose 29 but not 30 or 31.

 

Hope this gets you off to a good start.

 

Is actually about js. I got a library function to do all these. hehe. Mind I post it here?

 

function setDefaultSelectValue(ele, default_value)
{
if(typeof(ele) != 'object')
	ele = document.getElementById(ele);

if(typeof(ele) != 'object')
	return false;

for(var i=0; i<ele.options.length; i++)
{
	if(ele.options[i].value == default_value)
	{
		ele.options[i].selected = true;
		return true;
	}
}

return false;
}

 

so the ele parameter you can either put an id of your select or drop-down list element or you can put an object of your select element.

 

the default_value is what is the default value you want the select value to point at.

 

Usage:

// assume you got a drop-down-list with 1-31 days, with an id 'day'
<script language='javascript' type='text/javascript'>
setDefaultSelectValue('day', <?php echo date('d'); ?>);
</script>

 

That's it...

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.