无法从简单的新手表单中获取$ _SESSION中的信息

0 php session

在一种形式中,我向用户询问他的名字,我使用POST并将用户发送到FormTwo.php并将$ _POST ["name"]保存到会话中.

在第二个中我要求他的姓氏并做同样的事情并将他发送到registerComplete.php页面.

我试图回应他写的所有信息,根据我的理解应该可以通过$ _SESSION数组访问,对吗?

我究竟做错了什么?:)

RegisterFormOne.php:

<?php session_start(); ?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 1 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>
        <form action="registerFormTwo.php" method="post">
            <dt>First Name:</dt>
                <dd><input type="text" name="firstName" /></dd><br />

            <dt>Last Name:</dt>
                <dd><input type="text" name="lastName" /></dd><br />

            <dt>Age:</dt>
                <dd><input type="text" name="age" /></dd><br />

            <dt>Date of Birth:</dt>
                <dd><input type="text" name="dateOfBirth" /></dd><br />

            <dt>Gender:</dt>
                <dd>Masculino <input type="radio" value="M" name="gender" checked/> &nbsp;&nbsp;
                Femenino <input type="radio" value="F" name="gender" />
                </dd>
            <dt><input type="submit" /></dt>
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

RegisterFormTwo.php

<?php
if ($_POST){
  $_SESSION["firstName"] = $_POST["firstName"];
  $_SESSION["lastName"] = $_POST["lastName"];
  $_SESSION["age"] = $_POST["age"];
  $_SESSION["dateOfBirth"] = $_POST["dateOfBirth"];
  $_SESSION["gender"] = $_POST["gender"];
}
?>
<html>
    <head>
        <title>Registration Form - 2 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 2 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>               

        <form action="registerFinish.php" method="post">
            <dt>Nationality:</dt>
                <dd><input type="text" name="nationality" /></dd><br />

            <dt>Profession:</dt>
                <dd>
                    <select name="profession">
                        <option value="sistemas" selected="selected">Ing. Sistemas</option>
                        <option value="electrico">Ing. Electrico</option>
                        <option value="marketing">Marketing y Publicidad</option>
                        <option value="comercio">Comercio</option>
                    </select>
                </dd><br />

            <input type="submit" />
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

registerFinish.php

<?php
if ($_POST){
  $_SESSION["nationality"] = $_POST["nationality"];
  $_SESSION["profession"] = $_POST["profession"];
}
?>
<html>
    <head>
        <title>Registration Complete</title>
    </head>

    <body>
        <h1>Thank you for taking the census.</h1>
        <p>Please take a moment to review your inputted information and confirm when you feel everthing is OK.</p>               

        <ul>
            <li><?php /*This should echo his first name, but nothing shows. */ echo $_SESSION["firstName"]; ?></li>

        </ul>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ond*_*ták 5

你错过session_start();了其他2个文件的开头.

  • 不,它没有,它恢复了当前的那个.从php.net"session_start()根据通过GET或POST请求传递的会话标识符创建会话或恢复当前会话,或通过cookie传递." (2认同)