I have found online the following PHP function on defining a calendar drop-down (in month/day/year format). The function is working fine, but I would like:
a) Keep the selected value for Month, Day and Year, so to be able to edit values easily (values are defined dynamically through a foreach loop), and
b) would like to re-use the function on multiple variables (so far, I am using it to define date of birth, but I would like to use the same function also on additional fields too).
The fact that the values for Month, Day and Year are defined dynamically is a little bit confusing me, and of course I am not fluent in functions definition yet. Any help would be greatly appreciated !
Here is the function:
function make_calendar_pulldowns() {
// Make the months array:
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// Make the months pull-down menu:
//echo '<p><label for="dob" style="font-family: Verdana, Arial; font-size: 1.0em; font-weight: 600; color: #595959; line-height: 1.9em;">Date of Birth</label></p>';
echo '<select required="required" name="month" id="month" style="width: 33%; display: inline; float: left; margin-left: 1%; margin-right: 0%">';
echo '<option selected value="">Month</option>
';
foreach ($months as $key => $value) {
echo "<option value=\"$key\" >$value</option>
";
}
echo '</select>';
// Make the days pull-down menu:
echo '<select required="required" name="day" id="day" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
echo '<option selected value="">Day</option>
';
for ($day = 1; $day <= 31; $day++) {
echo "<option value=\"$day\">$day</option>
";
}
echo '</select>';
// Make the years pull-down menu:
echo '<select required="required" name="year" id="year" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
echo '<option selected value="">Year</option>
';
for ($year = 1910; $year <= 2000; $year++) {
echo "<option value=\"$year\">$year</option>
";
}
echo '</select>';
Once values have been defined, I convert them to MySQL format with the following code:
// Validate the month.
if (is_numeric ($month)) {
$dob = $month . '-';
} else {
$action['result'] = 'error';
array_push($text,'Please insert a valid Month for patient birth date');
}
// Validate the day.
if (is_numeric ($day)) {
$dob .= $day . '-';
} else {
$action['result'] = 'error';
array_push($text,'Please insert a valid Day for patient birth date');
}
// Validate the year.
if (is_numeric ($year)) {
$dob = $year . '-' . $month . '-' . $day; // Set Birthdate in SQL format
Finally, I call the function in the form with the following code:
<!-- <p><label for="dob">Date of Birth <img src="../img/req.gif" alt="Required"</label></p>
<?php make_calendar_pulldowns(); ?> -->