I have a site where I have users put in data for events. In that site I have a dropdown for the month, another for the days of the months, and another for year. The month and year I can set up with a static array of them. However, since each month has a different number of days, I want that month to only show the number of days for that particular month.
I have tried looking for a solution, but, well, did not find what I believe is a working solution.
I would like this to be a simple as possible and to use mostly - if not completely - PHP but do realize that some javascript might be necessary.
Thanks
E
‘some javascript’ would definately be necessary if you dont want to reload the page - PHP has no ability to manipulate a page once it has been rendered to the browser. an AJAX call to another PHP page would work fine; that page can call the function [FPHP]cal_days_in_month[/FPHP].
Thanks StarLion, but I (1) don’t want to navigate away from the page and, (2) have to refresh (one of my pet peeves is page refreshing “for no apparent reason”). So, might need some javascript but not sure how or what to do/use.
E
Well the easiest way to do it, I would say, would be to generate your months normally through PHP and then attach some JavaScript that updates the day dropdown with the correct number of days for the selected month.
So when the month dropdown changes, update the day select to have the correct number of fields. This would be a fairly simple thing to achieve if you know basic JavaScript.
Totally untested, and written on the fly, but something like this:
<select name="month" onchange="setDay(this.value)">
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
... and so on
</select>
<select name="day" id="day">
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
... and so on to 31; probably don't need these at all to be honest ...
</select>
Then a javascript function called setDay() which will update the day dropdowns options
function setDay(v)
{
// Latch onto the select we are updating by ID (day)
var select = document.getElementById("day");
// Set end day depending on the month (you could use IF statements to combine months actually)
switch(v)
{
case "january":
// Set end day to 31
endday = 31;
break;
case "febuary":
// Set end day to 29 (you'll need to account for leap years)
endday = 29;
break;
case "march":
// Set end day to 31
endday = 31;
break;
}
// Loop from 1 to endday creating select options
for(i=1;i<=endday;i++)
{
select.options[i] = new Option(i, i, true, false);
}
}
Something like that anyway.
http://www.w3schools.com/ajax/ajax_aspphp.asp
A quick example on how to AJAX-call a dynamic page. I’d recommend reading that entire thing from the beginning if you want to learn what it’s doing.
FizixRichard, tried your example and, well, it kinda works just does not put them in the listbox.
StarLion, I read and re-read your link and am still very confused.
Thanks
E