Hi all,
I am struggling with a javascript problem..
I have two drop down boxes. the first is Month, second has Days. The number of days in the box (eg: 28,29,30,31) depends on which month is selected, so march would be 31,april would have 30 days etc…
Most of my work is in PHP, but this sort of thing cant be achieved without sending the form to the server, which i dont want to do, i need it to be dynamic..
I need to reload the page to reflect the changes, but when i do, the variable resets it self i think…
please see the following code….this section is in the <head> tag of my .php file..
<script language=”javascript”>
<!–
var days;
var reloaded = false;
function calc_days(month)
{
switch(month)
{
case 1: days = 31;
break;
case 2: days = 28;
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
default: alert(“missed all cases”);
break;
}
reloaded = true;
window.location.reload(true);
}
function show_days(numDays)
{
for(var i = 1; i <= numDays; i++)
{
document.write(“<option>” + i + “</option>”);
}
}
//–>
</script>
How this is executed:
The month dropdown box has onchange=”calc_days(this.selectedValue)”….
and to display the days drop down box i have:
<select name=”days”>
<option SELECTED><–></option
<script language=”javascript”>
<!–
if(reloaded == true)
{ show_days(days); alert(“done with days variable:”); // debug statement
}
else
{
show_days(31); alert(“done without days:”); // debug statement
}
//–>
</script>
</select>
The show_days function relies on the reloaded variable to be true to excecute with the days variable….The problem is, when i have reloaded the page, both variables are gone because of the nature of javascript..
Does anyone have an alternative for select boxes where the 2nd one’s options depend on the value selected in the first one??
Any suggestions greatly appreciated..
Dave