从表单中保存会话变量

时间:2018-01-21 07:46:48

标签: php session variables

需要帮助在我尝试使用

的表单中保存会话变量中的POST方法
    <?php
    session_start();
    $favcolour = $_POST["favColour"];
    $_SESSION["favColour"] = $favcolour; 
    ?>

    <form action="exercise3.php" method="POST">
    <label for="fColour">Favourite Colour: </label><input type="text" 
    name="fColour" id="fcolour">
    <input type="submit" name="submit" value="Submit">
</form>

然后我需要在下一页回显变量

    session_start();
    if (isset($_POST['Submit'])) { 
    echo $_SESSION["fColour"];
    } 

2 个答案:

答案 0 :(得分:1)

您将其设置为 $_SESSION["favColour"],但回复为$_SESSION["fColour"]; 会话变量

中的名称必须相同

答案 1 :(得分:0)

<?php
    session_start();
    $favcolour = $_POST["favColour"];
    $_SESSION["favColour"] = $favcolour;   //You have set your session in favColour variable
?>

<form action="exercise3.php" method="POST">
    <label for="fColour">Favourite Colour: </label><input type="text" 
    name="fColour" id="fcolour">
    <input type="submit" name="submit" value="Submit">
</form>

你的exercise3.php代码。

session_start();
if (isset($_POST['Submit'])) { 
    echo $_SESSION["favColour"];  //Get the session value. The variable name should be same as what you have set.
}