Script 2.6 Using Arrays

Using simple Array constructs to simulate a calendar form.

Output

Source
<?php
$month = (isset($_POST['month']))?$_POST['month']:null;
$day = (isset($_POST['day']))?$_POST['day']:null;
$year = (isset($_POST['year']))?$_POST['year']:null;
$submit = (isset($_POST['submit']))?$_POST['submit']:null;

if($submit) {
	if(!empty($month) && !empty($day) && !empty($year)) {
		echo "<p>What's happenin $month $day, $year ?";
	}
} ?>
<form action="#" method="post">
	<?php # Script 2.6 - calendar.php

	// This script makes three pull-down menus
	// for an HTML form: months, days, years.

	// Make the months array:
	$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

	// Make the days and years arrays:
	$days = range (1, 31); //ideally this array would be dependent on the month and year selected!
	$years = range (2011, 2021);

	// Make the months pull-down menu:
	echo '<select name="month">';
	foreach ($months as $key => $value) {
		echo "<option value=\"$value\">$value</option>\n";
	}
	echo '</select>';

	// Make the days pull-down menu:
	echo '<select name="day">';
	foreach ($days as $value) {
		echo "<option value=\"$value\">$value</option>\n";
	}
	echo '</select>';

	// Make the years pull-down menu:
	echo '<select name="year">';
	foreach ($years as $value) {
		echo "<option value=\"$value\">$value</option>\n";
	}
	echo '</select>';
	?>
	<p align="left"><input type="submit" name="submit" value="Submit My Date" /></p>
</form>