如何使用PHP在新页面中显示结果表


how can I display the result table in a new page by using PHP?

尝试在新页面中显示结果,但不起作用。我提交数据后出现错误。总是说定义函数。我对编程很陌生,请帮帮我。谢谢。

    <html>
    <?php
    //define variables and set to empty values
    $firstNameErr = $lastNameErr = $ageErr = $areaStudyErr = "";
    $firstName = $lastName = $age = $areaStudy ="";
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
       if (empty($_POST["firstName"]))
         {$firstNameErr = "First Name is required";}
       else
         {
         $firstName = test_input($_POST["firstName"]);
         // check if section only contains letters and whitesapace
        if (!preg_match("/^[a-zA-Z ]*$/",$firstName))
           {
           $firstNameErr = "Only letters and white space allowed";
           }
         }
       if (empty($_POST["lastName"]))
         {$lastNameErr = "Last Name is required";}
       else
         {
         $lastName = test_input($_POST["lastName"]);
         // check if section only contains letters and whitesapace
        if (!preg_match("/^[a-zA-Z ]*$/",$lastName))
           {
           $lastNameErr = "Only letters and white space allowed";
           }
         }
        if (empty($_POST["age"]))
         {$ageErr = "Age is required";}
       else
         {
         $age = test_input($_POST["age"]);
         // check if age only contains numbers
       if (!is_numeric($age))
           {
           $ageErr = "Only numbers allowed";
           }
         }
         if (empty($_POST["areaStudy"]))
         {$areaStudyErr = "required section";}
       else
         {$areaStudy = test_input($_POST["areaStudy"]);  }
    }    
         function test_input($data)
    {
         return $data;
    }
    ?>
    <body>
    <h2>Student information</h2>
    <form method="post" action="submit.php" >
        First Name: <input type="text" name="firstName">
        <span class="error"> *<?php echo $firstNameErr;?> </span>
        <br></br>
        Last Name: <input type="text" name="lastName">
        <span class="error"> *<?php echo $lastNameErr;?></span>
        <br></br>
        Age: <input type="text" name="age">
        <span class="error"> *<?php echo $ageErr;?></span>
        <br></br>
        Area of Study: <input type="text" name="areaStudy">
        <span class="error"> *<?php echo $areaStudyErr;?></span>
        <br></br>
        <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>

文件submit.php中的代码,尝试使用它在新页面中显示表格结果

<html>
    <body>
    <table border="1" cellspacing=0 cellpadding=3>
    <tr>
    <th>Frist Name</th>
    <th>Last Name</th>
    <th>Age</th>
    <th>Area of Study</th>
    </tr>
    <tr>
    <td><?php echo $firstName; ?></td>
    <td><?php echo $lastName; ?></td>
    <td><?php echo $age; ?></td>
    <td><?php echo $areaStudy; ?></td>
    </tr> 
    </table>
    </body>
    </html>

在显示$_POST值之前,必须将它们分配给submit.php中的变量。

例如

$firstName=$_POST["firstName"];

您在这里犯了一些非常基本的错误,这让我建议您最好先把代码放在一边,花点时间在code Academy这样的网站上:http://www.codecademy.com/tracks/php

也就是说,您的错误是由于尝试使用submit.php中从未声明过的变量(作为错误状态)而导致的。这些变量在第一个页面(包含表单)中声明,并且在处理页面时仅存在于服务器上。

表单提交是一个全新的HTTP请求。由于HTTP是一种无状态协议,因此在原始请求期间存在的任何内容都不再存在。因此,要访问这些变量,您需要复制与原始页面上相同(或相似)的逻辑。