Hi there.... I have a problem. I have a form with two drop down menus. One has the days of a month and the other has the name of the month When someone changes the month I want the days to change so that there is only the amount of days for the chosen month This is what I have so far <label for="event_date_day">Date:</label> <select name="event_date_day" id="event_date_day"> <?php for($i=1;$i<=$daysInMonth;$i++) { ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php } ?> </select> <select name="event_date_month" id="event_date_month"> [INDENT]<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>Decemeber</option>[/INDENT] </select><br /> Code (markup):
There's no "onchange" event in PHP. If you want this to happen without submitting the form you have to ask in the JavaScript section. So do you want to do this with PHP or JavaScript?
There is a onChange event of the <select> form though. I thought there might be a way in PHP but I will try it through Javascript. Thanks
PHP does nothing after the output has been sent. It is a "pre-processor". Simplest method using inline behaviour: <script type="text/javascript"> var days_in_month = Array(31,29,31,30,31,31,30,31,30,31); // deal with leap years if you want :D function byId(id){ return document.getElementById ? document.getElementById(id) : document.all[id]; } function populateDays(month) { // validate month number here var days = days_in_month[month]; for (i = byId('days').options.length - 1; i>=28; i--) { byId('days').remove(i); } for (i = 28; i <= days; i++) { var option = document.createElement('option'); option.text = option.value = i; try { byId('days').add(option, null); } // non-IE catch(e) { byId('days').add(option); } // IE } } </script> <select onchange="populateDays(this.selectedIndex-1)"> <option>---</option> <option>January</option> <option>February</option> </select> <select id="days"> <option>---</option> <option>1</option> <option>2</option> ... (fill this list to provide non JS fallback) </select> Code (markup):