Welcome to WebmasterWorld Guest from 54.226.189.112

Forum Moderators: brotherhood of lan & mack

Message Too Old, No Replies

Managing Long Drop Down Lists

Long drop down lists

     

naiquevin

2:37 pm on Mar 19, 2009 (gmt 0)

5+ Year Member



I want to allow users to select their Date of Birth using 3 drop down lists. Date/Month/ Year.
The Year list will be really long .. 1900 -2009 ..
So does it mean that I ll have to write the option tags 110 times!
Or is there a shortcut method available for this ?

Also, if I chose to keep these drop down lists in 3 separate files and include them in the form, will there be any problem in passing the variables using the post method ?

Thanks !

rocknbil

3:55 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



There is a shortcut - copy and paste. :-) But better yet, you reveal . . .

...and include them in the form, will there be any problem in passing the variables using the post method

... that you have some access to server-side programming. I'll guess at PHP. Short answer, sure you can do that, but then your lists are hard-coded. There's a better way; this is not "working code" but gives you the idea:

$year_start = '1900';
$year_end = '2009';
$ddName = 'birth'; // or convert from input variables
// What you REALLY should do is get the current year
// dynamically and use that for year end.

$moName = $ddName . '_month';
$daName = $ddName . '_day';
$yrName = $ddName . '_year';

// Do some stuff here to set the values for $mm, $dd,
// $yyyy so you can set "selected" if it's present


$dtString = "
<select name=\"$moName\" id=\"$moName\">
<option value=\"\">--</option>
";
for ($i=1;$i<=12;$i++) {
$moTxt = (strlen($i)<2)?'0' . $i:$i;
$dtString .= "<option value=\"$moTxt\"";
if ($mm == $moTxt) { $dtString .= ' selected'; }
$dtString .= ">$moTxt</option>\n";
}
$dtString .= "
</select>
";

//(rinse and repeat for DAY, using 1-31)


$dtString .= "
<select name=\"$yrName\" id=\"$yrName\">
<option value=\"\">--</option>
";
for ($i=$start_year;$i<=$end_year;$i++) {
$dtString .= "<option value=\"$i\"";
if ($yyyy == $i) { $dtString .= ' selected'; }
$dtString .= ">$i</option>\n";
}
$dtString .= "
</select>\n";

Now output $dtString and you have your date selects . . . . apply the same concepts for a time list.

dreamcatcher

8:58 am on Mar 22, 2009 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Another alternative would be a small javascript calendar that pops up when a form field is clicked. This eliminates 3 drop downs and makes things nice and tidy.

dc

 

Featured Threads

Hot Threads This Week

Hot Threads This Month