dousui7410 2016-03-21 19:04
浏览 38

尝试使用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.

  • 写回答

2条回答 默认 最新

  • dsv17139 2016-03-21 19:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP