我的testing2.php有什么问题

时间:2016-04-08 06:24:26

标签: php forms

这是我的php表单代码......

 <?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Page Title Goes Here</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="form1.css"/>
    </head>
    <body>
        <?php

            // define variables and set to empty value
            $firstNameError = "";
            $lastNameError = "";
            $error = false;

            // if firstName is empty, make it NULL, else, test_input() the data.
            $firstName = empty($_POST["firstName"]) ? NULL : test_input($_POST["firstName"]);

            // if lastName is empty, make it NULL, else, test_input() the data.
            $lastName = empty($_POST["lastName"]) ? NULL : test_input($_POST["lastName"]);

            if (isset($_POST["submittingForm"])) {

                /// CHECK FIRST NAME ERRORS
                if ($firstName === NULL) {
                    // firstName is empty
                    $firstNameError = "First name is required!";
                    $error = true;

                } else {
                    // check characters
                    if (!preg_match("/^[a-zA-Z ]*$/", $firstName)) {
                        $firstNameError = "Only letters and white spaces allowed!";
                        $error = true;
                    }
                }

                /// CHECK LAST NAME ERRORS
                if (!preg_match("/^[a-zA-Z ]*$/", $lastName)) {
                    // check characters
                    $lastNameError = "Only letters and white spaces allowed!";
                    $error = true;
                }

                // if no error then redirect
                if (!$error) {
                    $_SESSION['fistName'] = $firstName;
                    $_SESSION['lastName'] = $lastName;
                    header('Location: testing2.php');
                    exit();
                }

            } else {
                // user did not submit form!
            }

            // clean input
            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }

        ?>
        <div id="wrapper">
            <h1>Welcome to Chollerton Tearoom! </h1>
            <form id="userdetail" method="POST">
                <fieldset id="aboutyou">
                    <legend id="legendauto">user information</legend>
                    <p>
                        <label for="firstName">First Name: </label>
                        <input type="text" name="firstName" id="firstName" value="<?php echo $firstName; ?>">
                        <span class="error">* <?php echo $firstNameError;?></span>

                        <label for="lastName">Last Name: </label>
                        <input type="text" name="lastName" id="lastName" value="<?php echo $lastName; ?>">
                        <span class="error">* <?php echo $lastNameError;?></span>
                    </p>
                    <p> 
                        <input type="submit" name="submittingForm" value="submit">
                    </p>
                </fieldset>
            </form>
        </div>
    </body>
</html>

所以这是我的testing2.php,它在提交后获取数据......

    <?php 

session_start();

$firstName = $_SESSION['firstName'];
$lastName = $_SESSION['lastName'];


echo "<h1>Successfull submission :</h1>";
echo "<p>fitstName :  $firstName <p/>";
echo "<p>lastName :  $lastName <p/>";
?>

当表单被提交时,注意:第5行的F:\ xampp \ htdocs \ en407b \ assigment \ testing2.php中的未定义索引:firstName出现... 我试图解决它,但仍然没有把它弄好...... 请帮助我......

3 个答案:

答案 0 :(得分:1)

像这样改变

    if (!$error) {
                $_SESSION['firstName'] = $firstName;//error here
                $_SESSION['lastName'] = $lastName;
                header('Location: testing2.php');
                exit();
            }

答案 1 :(得分:0)

你有一个简单的拼写错误 -

if (!$error) {
    $_SESSION['firstName'] = $firstName;

应该是:

<form id="userdetail" action="testing2.php" method="POST">

我还建议给你的表单一个动作:

$_SESSION['fistName']

然后将所有表单处理逻辑移到该文件中。这将消除对标题重定向的需要。

答案 2 :(得分:0)

您的会话名称中有拼写错误。您正在将会话分配给$_SESSION['firstName'];并尝试使用public ReportQueryData (){ }

进行访问