Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  167] [ 7]  / answers: 1 / hits: 25942  / 13 Years ago, wed, may 4, 2011, 12:00:00

I am having problem on how to load the HTML <select> with values whenever the page is first loaded or when refreshed.



I am actually trying to create a date_select with 3 dropdown menus which includes year, month, and days in which when the month-menu or year-menu is changed it will automatically adjust the number of days.



For example, when I selected the month March the days will be 31 or April the days will be 30 only.



My problem is when the page is first loaded the day-menu is empty. I tried onload on the <select> but it didn't work. I am a beginner in programming so I am having trouble even simple exercises like this. Please help.



date_select.php



<script type=text/javascript >

function loadDays() {
var year = parseInt(document.getElementById('years').value);
var month = document.getElementById('months').value;
var months = new Array(January, February, March, April, May, June, July, August, September, October, November, December);
var days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

if ((year % 4) == 0) days[1] = 29;

var days_select = document.getElementById('days');
days_select.options.length = 0;

for(i in months) {
if (month == months[i]) {
for(var j = 1; j <= days[i]; j++ ) days_select.options[days_select.options.length] = new Option(j, j);
break;
}
}
}

</script>

<span style=display: table; >
<span style=display: table-cell; >
<select id=years onchange=loadDays();>
<?php
for($year = 1900; $year <= 2100; $year++ ) {
if ($year == date('Y')) echo <option value='$year' selected=''> . $year . </option>;
else echo <option value='$year'> . $year . </option>;
}
?>
</select>
</span>
&nbsp;
<span style=display: table-cell; >
<select id=months onchange=loadDays();>
<?php
$months = array(January, February, March, April, May, June, July, August, September, October, November, December );

foreach($months as $month){
if ($month == date('M')) echo <option value='$month' selected=''> . $month . </option>;
else echo <option value='$month'> . $month . </option>;
}
?>
</select>
</span>
&nbsp;
<span style=display: table-cell; >
<select id=days onload=loadDays();>

</select>
</span>
</span>

More From » php

 Answers
61

Try putting the onload attribute in the body tag instead of the select tag.



<body onload=loadDays();>

[#92418] Monday, May 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frederickmohamedw

Total Points: 21
Total Questions: 123
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
frederickmohamedw questions
Wed, Sep 23, 20, 00:00, 4 Years ago
Sat, Jul 18, 20, 00:00, 4 Years ago
Sun, Apr 26, 20, 00:00, 4 Years ago
Sat, Jan 11, 20, 00:00, 4 Years ago
Fri, Dec 27, 19, 00:00, 4 Years ago
;