尝试使用PHP动态重定向表单页面输出

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.

Don't change the action on the HTML itself. Use PHP to redirect the user from the action script.

E.g.

TheForm.php

<form method='POST' action='MyScript.php'>
    <input type='text' name='email' />
</form>

MyScript.php

<?php
if(valid($_POST['email'])){
    header('Location: AllIsGood.php');
} else {
    header('Location: NotValid.php');
}
?>

AllIsGood.php

<p>Thanks! Your email was valid.</p>

NotValid.php

<p>Oh no! You gave an invalid email.</p>

Regarding your code, in case you didn't realize, the top PHP code will execute whenever the page (that contains the form) loads. If you set the action of the form to the page the form is on (i.e. call itself), the top PHP where you set your initial values will execute. The PHP wrapped in the $_SERVER["REQUEST_METHOD"] == "POST" logic will not wait for the user to fill out the form. It will execute whenever the REQUEST_METHOD is POST.

Your main page posts the form data to formverify.php, therefore the main page never sees the data or sets the session variables. Maybe you meant to set the form action to the current page rather than formverify.php?