title: How to load html <select> with values upon loading the page using javascript in PHP?
body: <p>I am having problem on how to load the HTML <code>&lt;select&gt;</code> with values whenever the page is first loaded or when refreshed. </p>
  
  <p>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. </p>
  
  <p>For example, when I selected the month March the days will be 31 or April the days will be 30 only. </p>
  
  <p>My problem is when the page is first loaded the day-menu is empty. I tried <code>onload</code> on the <code>&lt;select&gt;</code> but it didn't work. I am a beginner in programming so I am having trouble even simple exercises like this. Please help. </p>
  
  <p><b><code>date_select.php</code></b></p>
  
  <pre><code>&lt;script type="text/javascript" &gt;
  
   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 &lt;= days[i]; j++ ) days_select.options[days_select.options.length] = new Option(j, j);
      break;
     }
    }
   }
  
  &lt;/script&gt;
  
  &lt;span style="display: table; "&gt;
   &lt;span style="display: table-cell; "&gt;
    &lt;select id="years" onchange="loadDays();"&gt;
     &lt;?php 
      for($year = 1900; $year &lt;= 2100; $year++ ) {
       if ($year == date('Y')) echo "&lt;option value='$year' selected=''&gt;" . $year . "&lt;/option&gt;";
       else echo "&lt;option value='$year'&gt;" . $year . "&lt;/option&gt;";
      }
     ?&gt;
    &lt;/select&gt;
   &lt;/span&gt;
   &amp;nbsp;
   &lt;span style="display: table-cell; "&gt;
    &lt;select id="months" onchange="loadDays();"&gt;
     &lt;?php 
      $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
  
      foreach($months as $month){
       if ($month == date('M')) echo "&lt;option value='$month' selected=''&gt; " . $month . "&lt;/option&gt;";
       else echo "&lt;option value='$month'&gt; " . $month . "&lt;/option&gt;";
      }
     ?&gt;
    &lt;/select&gt;
   &lt;/span&gt;
   &amp;nbsp;
   &lt;span style="display: table-cell; "&gt;
    &lt;select id="days" onload="loadDays();"&gt;
  
    &lt;/select&gt;
   &lt;/span&gt;
  &lt;/span&gt;
  </code></pre>