PDA

View Full Version : populate a drop down menu form field with javascript, so as first menu item (eg in a



leonidassavvides
12-16-2008, 09:06 AM
How I populate a drop down menu form field with javascript, so as first menu item (eg in a date menues) be current month or day or year like php coding countries below ?


<select name="country" size="1" id="country" >
<option value="<?=$_SESSION['country']?>" selected="selected"><?=$_SESSION['country']?></option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Cyprus">Cyprus</option>
<option value="Greece">Greece</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
.............................
.............................

vwphillips
12-16-2008, 02:09 PM
not sure I understand but


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

var zxcMonths=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

function Test(zxcid,zxcfmt){
var sel=document.getElementById(zxcid);
var zxcnow= new Date();
var zxctxt=[zxcnow.getFullYear(),zxcnow.getMonth(),zxcnow.getDate()];
zxctxt[1]=zxcfmt.indexOf('a')>-1?zxcMonths[zxctxt[1]]:zxctxt[1]+1;
if (zxcfmt.indexOf('a')<0)zxctxt[1]=zxctxt[1]>9?zxctxt[1]:'0'+zxctxt[1];
zxctxt[2]=zxctxt[2]>9?zxctxt[2]:'0'+zxctxt[2];
zxcfmt=zxcfmt.charAt(0)=='m'?true:false;
sel.options[0].value=zxctxt[zxcfmt?1:2]+'/'+zxctxt[zxcfmt?2:1]+'/'+zxctxt[0];
sel.options[0].text=sel.options[0].value;
}

/*]]>*/
</script>
<body onload="Test('country','mm/dd/yyyy/a')">
<form>
<select name="country" size="1" id="country" >
<option value=""></option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Cyprus">Cyprus</option>
<option value="Greece">Greece</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
</select>
</form>

</body>

</html>

leonidassavvides
12-16-2008, 06:31 PM
I do not mean this , I mean like php I choose the first item in the form(populate it with data) to use javascript for populating , like eg a drop down shows current date(forget countiries drop down) ...


<td width="22%"><select name="pickUpMonth" style="font-size: 11px" tabindex="1">
<?php
$curdate = getdate(); // time()+6*60*60
$curday = $curdate['mday'];
$curmonth = $curdate['mon'];
$curyear = $curdate['year'];
$arr_months = array(1 =>
"January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");


foreach ($arr_months as $i => $monthname)
{
if ($i == $curmonth)
echo "<option value='$i' selected='selected'>$monthname</option>";
else
echo "<option value='$i'>$monthname</option>";
}
?>
</select></td>
<td width="22%"><select name="pickUpDay" tabindex="5" style="font-size: 11px">
<?php
/* $curdate = getdate();
$curday = $curdate['mday'];
$curmonth = $curdate['mon'];
$curyear = $curdate['year']; */
for ($i = 1; $i <= 31; $i++)
{
if ($i == $curday)
echo '<option value="', $i, '" selected="selected">', $i, '</option>';
else
echo '<option value="', $i, '">', $i, '</option>';
}
?>
</select></td>
<td width="19%"><select name="pickUpYear" style="font-size: 11px" tabindex="15">
<?php
/* $curdate = getdate();
$curday = $curdate['mday'];
$curmonth = $curdate['mon'];
$curyear = $curdate['year']; */
for ($i = 2008; $i <= 2010; $i++)
{
if ($i == $curyear)
echo '<option value="', $i, '" selected="selected">', $i, '</option>';
else
echo '<option value="', $i, '">', $i, '</option>';
}
?>
</select></td>
<td width="37%"><img src="/images/calendar.gif" alt="calendar icon" width="22" height="17" /></td>

I suppose js code must applied better to:

<select name="day" size="1" class="formfields" id="day" tabindex="22" ;>
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</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>
<select name="month" size="1" class="formfields" id="month" tabindex="23" ;>
<option value="1" selected="selected">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year" size="1" class="formfields" id="year" tabindex="25" ;>
<option value="2008" selected="selected">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
</select>

leonidassavvides
12-16-2008, 06:35 PM
The irst item (if today is 25 dec 2008) must be double in the list so day menu shows first item, and selected "25", month similarly "december" and year similarly
"2008" ... well ?