Forum Moderators: coopster & phranque

Message Too Old, No Replies

Setting the selected value on drop down menu

         

aerocan

5:12 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



Here is my new challenge.

On my form I have 3 different fields. One for the month "EventDateMM", one for the day "EventDateDD" and one for the year "EventDateYY". All have a list of possible choices. Day = 1 to 31, Month = 1 to 12 and Year = 2007 to 2010.

I would like that the form automatically select the actual day, month and year on the drop down menus.

Once again, I'm totally JAVA incompetent...

Thansk for your help.

AEROCAN

________________________

<div align="center">
<select class="fieldStd" name="EventDateMM" size="1">
<option></option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>&nbsp;</div>
</td>
<td nowrap>
<div align="center">
<select class="fieldStd" name="EventDateDD" size="1">
<option></option>
<option value="01">01</option>
<option value="02" 0>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>&nbsp;</div>
</td>
<td nowrap>
<div align="center">
<select class="fieldStd" name="EventDateYY" size="1">
<option value=" "></option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>

</select></div>

rocknbil

7:13 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well this is the perl forum, but you would be better off doing it in perl or PHP anyway, not Javascript. :-)

Here is the sub I use. It relies on mysql, but you can modify the get date part to use system time. You pass three values to it:

$ddDame = the base "name" of the three drop-down lists for month, day, year. So each list will be named event_month,event_day, event_year.

$inDate - if present, this will set the list to whatever date you sent to it, in mm/dd/yy or yyyy-mm-dd format. Useful for things like expirations, etc.

$useCurrentDate - set to 1 or nothing at all. If present, it will set the list to today's date.

Usage:

You **should** have $date_start and $date_end set as a global somewhere for the year list.

$date_start = '2007';
$date_end = '2025';

In this example, form values are stored in %qs. So if there is a form value in memory for $qs{'event_day'} . . . . . etc., it will override other values and set it to that value. Otherwise,

$dateLists = &dateList('event');
Lists will set to top (blank)
$dateLists = &dateList('event','2007-10-31');
Lists will set to Haloween 2007
$dateLists = &dateList('event','10/31/2007');
Ditto
$dateLists = &dateList('event','',1);
Sets to today's date

Then you just drop $dateLists in wherever you need them.
It's not perfect, but it works.


sub dateList {
my (@dtOrig,$moName,$daName,$yrName,$dtString,$yrStart,$ddName,$inDate,$useCurrentDate);
$moName= $daName= $yrName='';
($ddName,$inDate,$useCurrentDate) = (@_);
$moName = $ddName . '_month';
$daName = $ddName . '_day';
$yrName = $ddName . '_year';


## Get the dates worked out.
if (($qs{$moName}) && ($qs{$daName}) && ($qs{$yrName})) {
$mm = $qs{$moName}; $yyyy = $qs{$yrName}; $dd = $qs{$daName};
}
elsif ($inDate =~ /\//) { ($mm,$dd,$yyyy) = split(/\//, $inDate); }
elsif (($inDate =~ /\-/) && ($inDate ne '0000-00-00')) { ($yyyy,$mm,$dd) = split(/\-/, $inDate); }
else {
if ($useCurrentDate && ($inDate eq '')) {
$sth = $dbh->prepare("select curdate();");
$rv = $sth->execute or &die("Can't get current date");
($inDate) = $sth->fetchrow_array;
$sth->finish;
}
($yyyy,$mm,$dd) = split (/\-/,$inDate);
}
if ($yyyy < 100) { $yyyy += 1900; }


$dtString = qq/
<select name="$moName" id="$moName">
<option value="">--</option>
/;
for $i (1..12) {
$moTxt = (length($i)<2)?qq/0$i/:$i;
$dtString .= qq/<option value="$moTxt"/;
if ($mm eq $moTxt) { $dtString .= ' selected'; }
$dtString .= qq/>$moTxt</option>\n/;
}
$dtString .= qq/
</select>
<select name="$daName" id="$daName">
<option value="">--</option>
/;
for $i (1..31) {
$daTxt = (length($i)<2)?qq/0$i/:$i;
$dtString .= qq/<option value="$daTxt"/;
if ($dd eq $daTxt) { $dtString .= ' selected'; }
$dtString .= qq/>$daTxt</option>\n/;
}
$dtString .= qq/
</select>
<select name="$yrName" id="$yrName">
<option value="">--</option>
/;
if ((! $date_start) or (! $date_end)) { $date_start=$yyyy-10; $date_end=$yyyy+15; }
for $i ($date_start..$date_end) {
$dtString .= qq/<option value="$i"/;
if ($yyyy eq $i) { $dtString .= ' selected'; }
$dtString .= qq/>$i</option>\n/;
}
$dtString .= qq/
</select>\n/;


return $dtString;
}