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>

    So for some reason $htmlblock isn't getting its value. I see it's assigned to earlier, so for some reason those statements aren't getting run. Taking the full code, lopping out everything that doesn't cause the program to branch, and replacing all code that does anything with or to $htmlblock with just a note saying so:

    if ((empty ($_POST['day'])) || empty ($_POST['month']) || empty ($_POST['year']))
    {
    }
    else
    {
    
    $htmlblock = "";
    for ($m = 0; $m < count($monthArray)+1; $m++){
    	$htmlblock .= "";
    }
    $htmlblock .= "";
    
    for ($x = 0; $days = $x+1; $x++)
    {
    	$htmlblock .= "";
    }
    $htmlblock .= "";
    for ($y = 0; $y < count($yearArray)+1; $y++)
    {
    	$htmlblock .= "";
    }
    $htmlblock .= "";
    
    if ($check == 1)
    {
    	$htmlblock .= "<p>and the new TIMESTAMP version should show as $newtime</p>";
    }
    elseif ($check == 0) {
    } else {
    }
    }
    }
    echo $htmlblock;
    

    I notice that if ((empty ($POST['day'])) || empty ($POST['month']) || empty ($_POST['year'])), $htmlblock is never given a value; yet you still use it. Looking at what happens in that situation, I'd suggest putting that message into $htmlblock and letting it be displayed by the code at the bottom all fully HTML'd instead of just echoing it straight away.

    Oh, I also just noticed that if $check==0, you set a variable but then redirect the browser to request the page again; but after setting location headers, you should exit(), because otherwise program execution will just continue.

      WP, thanks for your help!

      i haven't tried anything yet-- so these are just questions i have from a first read of your reply:

      Weedpacket wrote:

      I notice that if ((empty ($POST['day'])) || empty ($POST['month']) || empty ($_POST['year'])), $htmlblock is never given a value; yet you still use it.

      are you sort of suggesting that i should give some value to $htmlblock here? if so, what then should that value be, and assigned w/ what code exactly?

      Weedpacket wrote:

      Looking at what happens in that situation, I'd suggest putting that message into $htmlblock and letting it be displayed by the code at the bottom all fully HTML'd instead of just echoing it straight away.

      but, doesn't that eliminate the whole point of this bit?
      if ($check == 1){ . . . header("location $_SERVER[PHP_SELF]");

      maybe i'll get it once i try playing with it, and running the script.

      Weedpacket wrote:

      you should exit(), because otherwise program execution will just continue.

      i actually meant to do that-- but i think i was gonig to use break(). but, i guess break is instead used for a while or for loop, etc.. i'll consult the manual, but if you have any commentary about potential confusion between those two functions, i'd appreciate it.

      thanks again!!

        here's what i've got so far for the updated version (going from WP's advice, as much as i was able to pickup on what WP was telling me)

        $htmlblock = "";
        
        isset($_POST['op']);
        
        if ($_POST['op'] == "makedate") {
        
        
        // FORMULA FOR CONVERT TO TIMESTAMP 
        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>";
        exit;
        } elseif ($check == 0) { 
        
        $htmlblock = "
        <p>You entered an invalid date! Please <a href=\"datecomp.php\">try again</a>:</p>";
        } elseif (empty($_POST['op'])) {
        
        // BEGIN MAKING HTML FORM
        $htmlblock .= "
        	<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
        
        // MAKE MONTHS ARRAY
        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>";
        
        // MAKE DAYS ARRAY	
        $htmlblock .= "<select name=\"day\">";
        for ($x = 0; $days = $x+1; $x++){
        
        $htmlblock .= "<option value=\"$days\">$days</option>";
        if ($x==30) {
        	break;
        }
        }
        $htmlblock .= "</select>";
        
        // MAKE YEARS ARRAY	
        $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;	
        }
        }
        
        // CLOSE FORM AND SUBMIT
        $htmlblock .= "</select>
        	<input type=\"hidden\" name=\"op\" value=\"makedate\"/>
        	<input type=\"submit\" name=\"submit\" value=\"Enter Date\" />
        
        </form>"; 
        
        
        } else {
        
        }
        } 
        
        
        
        ?>

        it's not quite right yet, but i'm currently tweaking it-- what i think are the final details... so, i should get it soon-- but i'd appreciate any help if you see something horribly erroneous in here that's going to cause me to run around in circles w/ this. thanks!

          well, there's probably a better way to accomplish this same outcome, but this works exactly as I want. I'd like to hear a critique of my solution of anyone feels like it.
          oh, you can probably tell that i'm unsure of the isset function-- i guess i just don't get it. hopefully, i'll come across a good example where it will come to me. to me, my line 4 is useless-- am i right? i used it as an original attempt to eliminate the Notice: undefined index op or other such Notices, but is that not why we use it... i didn't seem to do the trick anyway-- but i left it there just to illustrate that i need help in that area. thanks.

          <?php
          
          $htmlblock = "";
          if (isset($newtime, $_POST['op'], $day, $m, $month, $year, $_POST['day'])) {
          } else {
          
          }
          
          if (empty($_POST['op'])) {
          	$_POST['op'] = "";
          } else {
          }
          
          if ($_POST['op'] == "makedate") {
          
          // FORMULA FOR CONVERT TO TIMESTAMP 
          
          $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>
          <p>click <a href=\"datecomp.php\">here</a> to enter another</p>";
          
          } elseif ($check == 0) {		
          
          $htmlblock = "
          <p>You entered an invalid date! Please <a href=\"datecomp.php\">try again</a>:</p>";
          }	
          
          } elseif (empty($_POST['op'])) {
          
          // BEGIN MAKING HTML FORM
          $htmlblock .= "
          	<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
          
          // MAKE MONTHS ARRAY
          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>";
          
          // MAKE DAYS ARRAY	
          $htmlblock .= "<select name=\"day\">";
          for ($x = 0; $days = $x+1; $x++){
          
          $htmlblock .= "<option value=\"$days\">$days</option>";
          if ($x==30) {
          	break;
          }
          }
          $htmlblock .= "</select>";
          
          // MAKE YEARS ARRAY	
          $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;	
          }
          }
          
          // CLOSE FORM AND SUBMIT
          $htmlblock .= "</select>
          	<input type=\"hidden\" name=\"op\" value=\"makedate\"/>
          	<input type=\"submit\" name=\"submit\" value=\"Enter Date\" />
          
          </form>"; 
          
          
          } else {
          
          }
          
          
          
          
          ?>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
             "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="content-Style-Type" content="text/css" />
          <meta http-equiv="content-type" content="text/html;charset=utf-8" />
          <title>Crowbar Event Scheduler - Add a Date</title>
          <link rel="stylesheet" type="text/css" href="../css/php.css" />
          </head>
          <body>
          <div id="pagewidth">
          
          <?php 
          echo $htmlblock; 
          ?>
          
          <p><img src="../images/placeholder.jpg" height="15" width="80" alt="image here" id="testoutput" /></p>
          </div>
          </body>
          </html>
            Write a Reply...