Jump to content

creating dynamic dropdown boxes for date


chiefrokka

Recommended Posts

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.

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.