chiefrokka Posted February 16, 2008 Share Posted February 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted February 16, 2008 Share Posted February 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted February 16, 2008 Share Posted February 16, 2008 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... Quote Link to comment Share on other sites More sharing options...
chiefrokka Posted February 16, 2008 Author Share Posted February 16, 2008 ok thanks guys, i'll check them out soon. i'll probably try the php one just cuz i'm more familiar with it. Quote Link to comment Share on other sites More sharing options...
revraz Posted February 16, 2008 Share Posted February 16, 2008 This is how I do it for the current month, just do the same for the day/year $selected = ''; for ( $x=1; $x <= 12; $x++ ) { if ( date("n") == $x ) { $selected = ' selected'; } Quote Link to comment 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.