PHP:会话状态存储和检索

时间:2016-11-20 02:18:47

标签: php session

我似乎无法检索某些值。对于这个PHP程序,我必须存储我的名字,姓氏,电话号码,单选按钮列表的值,以及从选择框/下拉列表中选择计算机游戏。我要做的是从Ses1文件中存储这些值并从Ses2文件中检索它们。目前,我可以存储所有5个不同的值。当我点击“提交信息”按钮时,我只能检索名字,姓氏和电话号码值。如何从下拉列表中的单选按钮列表和计算机游戏中检索值。我目前的代码如下。

<?php

// we always have to start session state
session_start();

if(isset($_POST["firstNameTextBox"]))
{
    $_SESSION["firstName"] = $_POST["firstNameTextBox"];
    header("Location: Session2.php");
}
if(isset($_POST["lastNameTextBox"]))
{
    $_SESSION["lastName"] = $_POST["lastNameTextBox"];
    header("Location: Session2.php");
}
if(isset($_POST["telephoneNumberTextBox"]))
{
    $_SESSION["telephoneNumber"] = $_POST["telephoneNumberTextBox"];
    header("Location: Session2.php");
}
if (isset($_POST["occupation"])) 
{
    $_SESSION["staff"] = $_POST["occupation"];
    $_SESSION["sudent"] = $_POST["occupation"];
    $_SESSION["faculty"] = $_POST["occupation"];
    header("Location: Session2.php");

}
if(isset($_POST["games"]))
{
    $_SESSION["League of Legends"] = $_POST["games"];
    $_SESSION["Fallout 4"] = $_POST["games"];
    $_SESSION["Overwatch"] = $_POST["games"];
    $_SESSION["DOTA 2"] = $_POST["games"];
    header("Location: Session2.php");
}

?>

<html>
<head>
    <title>Ses1</title>
</head>
</head>
<body>
    <form method="post">
        <label for = "firstNameTextBox">Enter your first name:</label>
        <input type="text" name="firstNameTextBox" value="Put your first name here" />
        <br />
        <label for = "lastNameTextBox">Enter your last name:</label>
        <input type="text" name="lastNameTextBox" value="Put your last name here" />
        <br />
        <label for = "telephoneNumberTextBox">Enter your telephone number:</label>
        <input type="text" name="telephoneNumberTextBox" value="Put your telephone number here" />
        <br />
        <input type="radio" name="occupation" value = "staff" />
        <?PHP print $staff; ?>
        Staff
        <input type="radio" name="occupation" value= "student" />
        <?PHP print $student; ?>
        Student
        <Input type="radio" name="occupation" value= "faculty" />
        <?PHP print $faculty; ?>
        Faculty
        <br />
        <select name="games">
          <option value="">Select one computer game...</option>
          <option value="League of Legends">League of Legends</option>
          <option value="Fallout 4">Fallout 4</option>
          <option value="Overwatch">Overwatch</option>
          <option value="DOTA 2">DOTA 2</option>
        </select>
        <br />
        <input type="submit" value="Submit Information" />
    </form>
</body>

<html>
<head>
    <title>Ses2</title>
</head>
<body>
    <?php

    // again, make sure the session is available for use
    session_start();

    if(isset($_SESSION["firstName"]))
    {
        echo "Your first name is " . $_SESSION["firstName"];
    }
    echo "<br />";
    if(isset($_SESSION["lastName"]))
    {
        echo "Your last name is " . $_SESSION["lastName"];
    }
    echo "<br />";
    if (isset($_SESSION["telephoneNumber"]))
    {
        echo "Your telephone number is " . $_SESSION["telephoneNumber"];
    }
    echo "<br />";
    if (isset($_SESSION["occupation"]))
    {
        echo "Your occupation is " . $_SESSION["occupation"];
    }
    echo "<br />";
    if(isset($_SESSION["games"]))
    {
        echo "The computer game you've chosen was " . $_SESSION["games"];
    }

    ?>
</body>

1 个答案:

答案 0 :(得分:1)

  

您无需在SESSION数组中存储每个单选按钮/选择框选项。

     

您的SESSION数组索引应该匹配POST数组的索引。

例如: $_SESSION["occupation"] = $_POST["occupation"]而不是

`$_SESSION["staff"] = $_POST["occupation"];
$_SESSION["sudent"] = $_POST["occupation"];
$_SESSION["faculty"] = $_POST["occupation"];`

试试这个:

if (isset($_POST["occupation"])) {
    $_SESSION["occupation"] = $_POST["occupation"];
}

if (isset($_POST["games"])) {
    $_SESSION["games"] = $_POST["games"];
    header("Location: Session2.php");
}