Getting Form Values
The final task is to add an HTML form to the page that enables the user to choose a month to display. Adding the form itself is straightforward HTML, as shown in Listing 10.
Listing 10Adding a Form
<html> <head><title>Event Calendar</title></head> <body> <form action="calendar.php" method="get"> Choose a month: <select name="month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> / <select name="year"> <option value="2002">2002</option> <option value="2003">2003</option> <option value="2004">2004</option> <option value="2005">2005</option> </select> <input type="submit" value="Display Calendar" /> </form> <?php //Initialize the variables $numberofdays = 31; $month = 1; $year = 2003; ...
This code displays the form at the top of the page, as shown in Figure 8.
Figure 8 Adding a form to the page.
Notice that we've used this page as the action for the form. When the user submits the form, we'll come back to this page to display the appropriate information. Of course, to do that we'll need to be able to access the information submitted.
I could devote an entire article to HTML forms and PHP (and have, actually), but we'll just cover the basics here. To make the form values available, we need to import the request variables, as shown in Listing 11.
Listing 11Importing Form Values
... <option value="2004">2004</option> <option value="2005">2005</option> </select> <input type="submit" value="Display Calendar" /> </form> <?php //Initialize the variables import_request_variables('pgc', ''); if (!isset($month)) { $today = getdate(); $month = $today['mon']; $year = $today['year']; } $numberofdays = cal_days_in_month(CALENDAR_GREGORIAN, $month, $year); $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"); echo ("<h1>This Month's Events ($month/$year)</h1>"); ?> <table border="1"> <tr> ...
In this case, we're first importing three types of request variables:
post and get, from forms
cookies
At the moment we're only using the get values. We used an empty string ('') for the second argument in the import function, so this statement creates variables that match the form-variable names. We named them month and year on the form, so the server creates and populates the $month and $year variables.
You can also use the import function to prepend information to the name of the variable. For example, if we'd used this:
import_request_variables('g', 'g_');
the function would have created variables called $g_month and $g_year.
Next, we're checking whether a value was submitted for $month. The isset() function returns true if the variable exists, even if it has an empty string as its value. (A value of null returns false.) The explanation point (!) acts as a "not" operator, so the expression
!isset($month)
returns true if the value of $month has not been set. If that's the case, then we know that the form hasn't been submittedthe user is just pulling up the page directlyso we're using the getdate() function (which returns an array) to get today's date. We can then extract the current month and year to use.
Once we know the month and year we want to display, we need to be able to determine how many days are in the month, which we're doing with the cal_days_in_month() function. This function works with several calendar types, so the CALENDAR_GREGORIAN constant indicates which calendar to use. This constant is defined as part of the Calendar module that includes the cal_days_in_month() function.
Finally, we're adapting the page so that the month and year being displayed are indicated in the heading, as shown in Figure 9. We could also adapt the form to default to the same information, but I'll leave that as an exercise for you.
Figure 9 The final page.