I've successfully completed a script which allows me to enter date info using dropdown lists.
as a precaution, I'm using checkdate to verify that the date entered is valid. if it's not, i'm reloading the page using header("location $_SERVER[PHP_SELF]"); so the <select> boxes are once again displayed.
however, i'm trying to get a message to print to screen telling the user why he or she is seeing the date options again-- just to reiterate the fact that an invalid date was entered.
here's what i have, which isn't working. what am i doing wrong? thanks!
$check = checkdate($m, $day, $year);
if ($check == 1){
$newtime = date("M-d-Y", mktime(0, 0, 0, $month, $day, $year));
$htmlblock .= "<p>and the new TIMESTAMP version should show as $newtime</p>";
} else ($check == 0) {
$msg = "<p>You entered an invalid date! Please try again:</p>";
header("location $_SERVER[PHP_SELF]");
}
and at the top of the script, in the event that the page was reloaded:
if ((empty ($_POST['day'])) || empty ($_POST['month']) || empty ($_POST['year'])) {
$_POST['day'] = "";
$_POST['month'] = "";
$_POST['year'] = "";
$month = "";
$day = "";
$year = "";
echo $_GET[$msg];
} else {
using this code AS IS, produces the error: Undefined Variable: htmlblock
(htmlblock being the variable which displays the dropdowns if i remove the conditional statements as shown above)
to help you better understand the whole situation, i decided to paste the complete code-- but please NOTE that if i remove the conditional statements as listed above, and let the script run w/out notifying the user why the page reloads upon checkdate() returns 0, then everything seems to work just fine.
thanks for your help!
<?php
if ((empty ($_POST['day'])) || empty ($_POST['month']) || empty ($_POST['year'])) {
$_POST['day'] = "";
$_POST['month'] = "";
$_POST['year'] = "";
$month = "";
$day = "";
$year = "";
echo $_GET[$msg];
} else {
$htmlblock = "
<form method=\"post\">";
global $m;
$monthArray = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December');
$htmlblock .= "<select name=\"month\">";
for ($m = 0; $m < count($monthArray)+1; $m++){
$htmlblock .= "<option value=\"$m\">$monthArray[$m]</option>";
if ($m == 11) {
break;
} else {
}
}
$htmlblock .= "</select>";
$htmlblock .= "<select name=\"day\">";
for ($x = 0; $days = $x+1; $x++){
$htmlblock .= "<option value=\"$days\">$days</option>";
if ($x==30) {
break;
}
}
$htmlblock .= "</select>";
$yearArray = array('2005', '2006', '2007', '2008', '2009', '2010');
$htmlblock .= "<select name=\"year\">";
for ($y = 0; $y < count($yearArray)+1; $y++){
$htmlblock .= "<option value=\"$y\">".$yearArray[$y]."</option>";
if ($y == 5) {
break;
} else {
$yearnum = $y;
}
}
$htmlblock .= "</select>
<input type=\"submit\" name=\"submit\" value=\"Enter Date\" />
</form>";
isset($day, $month, $year, $_POST['day']);
$day = $_POST['day'];
$month = $_POST['month'] + 1;
$m = $month;
$year = $_POST['year'];
$year = $year + 2005;
$check = checkdate($m, $day, $year);
if ($check == 1){
$newtime = date("M-d-Y", mktime(0, 0, 0, $month, $day, $year));
$htmlblock .= "<p>and the new TIMESTAMP version should show as $newtime</p>";
} elseif ($check == 0) {
$msg = "<p>You entered an invalid date! Please try again:</p>";
header("location $_SERVER[PHP_SELF]");
} else {
}
}
?>
<html>
<body>
<?php
echo $htmlblock;
?>
</body>
</html>