2016-11-20 199 views
0

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

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

你不必存储会话阵列中的每个单选按钮/选择框的选项。

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

E.g:中$_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"); 
}