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

时间:2014-04-26 06:22:13

标签: php html forms validation

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

    <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>

2 个答案:

答案 0 :(得分:0)

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

E.G。

$ firstName = $ _POST [&#39; firstName&#39;];

答案 1 :(得分:0)

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

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

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