I'm having an issue with my form for creating a date/time. I want the pre-selected user input to be available within the text area if they need to change or update the date details of an Event.
I'm running into 2 problems:
1) When selecting the month, it reverts back to January. Because I want to upload the month data in MySQL format, I wasn't sure how to do this without removing my Month conversion code.
2) When refreshing the form, multiple "0's" are added to the minute and hour from my else statement, even though I set a maxlength="2" within the form.
if (array_key_exists('convert', $_POST)) {
$month = $_POST['month'];
$day = trim($_POST['day']);
$year = trim($_POST['year']);
$hour = trim($_POST['hour']);
$minute = trim($_POST['minute']);
$ampm = ($_POST['ampm']);
//Validates Days and Years into the correct format & gives range for year
if (empty($day) || empty($year) || empty($hour) || empty($minute) ) {
$error = "Please fill in all fields";
}
elseif (!is_numeric($day) || !is_numeric($year) || !is_numeric($hour) || !is_numeric($minute) ) {
$error = "Please use numbers only";
}
elseif (($day < 1 || $day > 31) || ($year < 1000 || $year > 9999) || ($hour < 1 || $hour > 12) || ($minute < 0 || $minute > 60) ) {
$error = "Please use valid date range";
}
else {
$day = $day <10 ? '0'.$day : $day;
$hour = $hour <10 ? '0'.$hour : $hour;
$minute = $minute <10 ? '0'.$minute : $minute;
$eventDate = "$month $day, $year at $hour:$minute $ampm";
}
}
<form id "form1" name="form1" method="post" action="">
<table>
<tr>
<td>
<p>
<label for="select">Month:</label>
<select name="month" id="month">
<?php
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
for ($i = 1; $i <= 12; $i++) {?>
<option value="<?php echo $i < 10 ? '0'.$i : $i; ?>">
<?php echo $months[$i-1]; ?>
</option>
<?php } ?>
</select>
<label for="day">Date:</label>
<input name="day" id="day" type="text" size="2" maxlength="2" value="<?php print "$day"; ?>" />
<label for="year">Year:</label>
<input name="year" id="year" type="text" size="4" maxlength="4" value="<?php print "$year"; ?>" />
<label for="hour">Time:</label>
<input name="hour" id="hour" type="text" size="2" maxlength="2" value="<?php print "$hour"; ?>" />
<label for="minute">:</label>
<input name="minute" id="minute" type="text" size="2" maxlength="2" value="<?php print "$minute"; ?>" />
<label for="ampm"></label>
<select name="ampm" id="ampm" value="<?php print "$ampm"; ?>" >
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
</p>
<p>
<input id="convert "type="submit" name="convert" value="Convert" />
</p>
</table>
</form>