0

I have two html select lists which displays months and years. Based on the selections I want a third select list to display the working days for that month and year (which I have stored in a mysql database) I'm not to familiar with javascript and jquery to do this so that is the portion I need help with, to auto populate the date field once the other two options have been selected.

<select name="month" class="selectList" id="month">
   <option>January</option>
   <option>February</option>
   <option>March</option>
   <option>April</option>
   <option>May</option>
   <option>June</option>
   <option>July</option>
   <option>August</option>
   <option>September</option>
   <option>October</option>
   <option>November</option>
   <option>December</option>
</select>

<select name="year" class="selectList" id="year">
   <option>2012</option>
   <option>2013</option>
   <option>2014</option>
   <option>2015</option>
   <option>2016</option>
</select>

<select name="date" class="selectList" id="date">
   <option>
           javascript to auto populate 
           multiple options from mysql database 
           based on previous two selections
   </option>
</select>
2
  • It must be in pure JavaScript or you accept jQuery? Commented Jul 26, 2013 at 19:34
  • @MarcB I didn't even see that, I'll try that real quick, and jQuery works of course, anything to make it work would be fine :)
    – Da11aS
    Commented Jul 26, 2013 at 19:36

1 Answer 1

0
<script type="text/javascript">
    $(document).ready(function() {
        $('#date').hide();
        $('#month, #year').change(function() {
            var month = $('#month').val();
            var year = $('#year').val();
            var days_in_month = new Date(year, month, 0).getDate();
            for ( var i = 1; i <= days_in_month; i++ ) {
                $('#date').append('<option value="' + i + '">' + i + '</option>');  
            }
            $('#date').show();
        });
    });
 </script>

Or save yourselve a whole bunch of time and use the Datepicker built into JQuery. You can then parse the day, month, year server side.

2
  • 1
    This doesn't actually display the 'working days' in a given month per the original question: the asker is probably looking to automatically filter out weekends, holidays, the days his company was closed for Christmas vaction last year, etc.
    – giaour
    Commented Jul 26, 2013 at 19:50
  • The problem with that is I need the specific dates for the month and year that are stored in my database.
    – Da11aS
    Commented Jul 26, 2013 at 19:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.