I'm trying to create a "dynamic redirect" for an HTML form page, so that if improper input is encountered (eg: empty input value), the Form "Enter" button will cause the Error page to load (formerr.php), but if the input is OK, the data Verify page (formverify.php) will load. Here's what I've tried:
<?php
session_start();
?>
<html>
<?php
$emailErr = "";
$email = "";
?>
<?php
$newpage = "formverify.php" ;
echo "$newpage" . "<br>" ;
$_SESSION["emailErr"] = "email OK" ;
echo "<br>" . $_SESSION["emailErr"] . "<br>" ;
?>
<body>
<form method="POST" action=<?php echo "$newpage" ; ?> >
<?php echo "$newpage" . "<br>" ;?>
e-mail:<span class="error"> *Important!</span><br>
<input type="text" name="email" value="<?php echo $_SESSION["email"]; ?>" ><br>
<input type="submit" name="enter" value="Enter">
</form>
<?php
$email = ($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if ( !filter_var($email, FILTER_VALIDATE_EMAIL)) //check for presence of a properly formed email address
{
echo "filter_var output = " . filter_var($email, FILTER_VALIDATE_EMAIL) . "<br>" ; //none of these "echo" statements print
$_SESSION['emailErr']= "Invalid email format";
echo $_SESSION['emailErr'] ;
$_SESSION['newpage'] = "formerr.php" ;
$newpage = "formerr.php" ;
echo "<br> new page = " . $newpage ;
}
}
?>
</body>
The Verify Page:
formverify.php:
<?php
session_start();
?>
<?php
echo "Form Verify Page" . "<br>" ;
$_SESSION['email'] =$_POST['email'];
echo "emailErr = " . $_SESSION['emailErr'] ;
echo "<br> email is: " . $_SESSION['email'];
echo "<br> Session emailErr = " . $_SESSION['emailErr'];
echo "<br> new page = " . $_SESSION['newpage'] ;
?>
</html>
Value for "$_SESSION['email']" comes across correctly if filled in, blank if not filled in.
"$_SESSION['emailErr']" remains as "email OK" regardless. (?)
"$_SESSION['newpage']" is blank (empty) regardless of input (?)
Even when the "email" text box is empty, the form passes to the "formverify.php" page, and the "newpage" variable echos as blank .
I'm not really clear on how the HTML "Form" acts when the Input Button is activated, but I expected it to trigger a reset of the $newpage variable based on the PHP script below it, but it doesn't change the value of $newpage - it remains as "formverify.php" instead of being changed to "formerr.php . I'm a total self-taught noob, so please excuse any lack of understanding.