views:

536

answers:

4

How would I generate parts of a form using PHP?

I would like to make a form that allowed the user to enter a date through drop-down menus of month, day, and year respectively without typing every single option of the menu, how would I automatically do so with PHP?

For example, how would I generate the days 1-31 for January, 1-28 for February, and so onwards without manually entering them using pure HTML?

+2  A: 

Here's a simplified version of how I do it in PHP

<select name="birthday_month" id="birthday_month">
    <option value=""></option>
    <? foreach(range(1,12) as $month): ?>
    <option value="<?= $month ?>"><?= date('F', mktime(1,1,1,$month,1,2000)) ?></option>
    <? endforeach; ?>
</select>

<select name="birthday_day" id="birthday_day">
    <option value=""></option>
    <? foreach(range(1,31) as $day): ?>
    <option value="<?= $day ?>"><?= $day ?></option>
    <? endforeach; ?>
</select>

<select name="birthday_year" id="birthday_year">
    <option value=""></option>
    <? foreach(range(date('Y'),1900) as $year): ?>
    <option value="<?= $year ?>"><?= $year ?></option>
    <? endforeach; ?>
</select>

You can create functions that output them if you are going to be using them a lot.

Edit: I don't take the time to autopopulate the days because the date should still be checked server side.

Galen
A: 

I think HTML5 has got a date selector component, but maybe it's too early for using that.

Galen is on the right track, but as you change from one month to the other in the browser, you won't get the correct date settings (ie: you'll end up with February 31st if you don't call back to the server)

Itay Moav's Javascript suggestion I believe is the way to go: Use javascript to modify the DOM at the browser end. When the user selects February, triggerring an onChange event for the month selector, you would rewrite the date selector, and create 28-29 days, depending on the year (!)

I did something along these lines once, you should be able to pull apart the page/script without too much difficulty (The site's in Japanese, but the source code is poorly written).

Assembler
You still have to check the date on the server side even if you use javascript to autopopulate the days of the month. My way is just a lot simpler.
Galen
it is much simpler... upvote!
Assembler
A: 

Two Answers:

1: Find a JavaScript Calendar class.

2: In PHP:

$year = 2009;  // February has different days in month - based on leap year
 for ($month = 1; $month <= 12; $month++)
   $lastDayOfMonth[$month] = date("j", strtotime("yesterday", strtotime($month+1."/1/$year")));

Now you have an array (map) of months, index by month, with values set to the number of days in the month;

The time code basically says this: Find the first day of the next month - then find yesterday (the day before the 1st day of the next month) - then return the day. So the last day of the month is the number of days in that month.

-CF

A: 

Also, if you want to display the number of days in the month the script is being executed, you can do:

print date("t", mktime(0,0,0, date("n") ));

This will output either 30, 31, or 28(if february)

lyrae