简体   繁体   中英

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

Try to display the result in a new page, but it doesn't work. there is error after I submit data.Always said defined function. I am very new at programming, please help me. Thank you.

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

code in file submit.php, try to use this to display the table result in new page

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

You must assign $_POST values to the variables at submit.php before displaying them.

EG

$firstName = $_POST['firstName'];

You're making some very basic mistakes here, which lead me to suggest that you might be better off putting your code aside and spend some time on a site like Code Academy first: http://www.codecademy.com/tracks/php

That being said, your error is caused by trying to use variables in submit.php that have never been declared (as the error states). Those variables were declared in the first page (which contains the form), and only exist on the server while the page is being processed.

A form submission is a brand new HTTP request . Since HTTP is a stateless protocol , anything that existed during the original request is no longer around. So, to have access to those variables, you need to duplicate the same (or similar) logic that you have on the original page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM