我想使用SESSIONS

时间:2016-06-09 00:35:21

标签: php html session

在food.php上选择submit并收集所有帖子数据后,将其发布在homePage.php上。 如何让$_POST['meal']保持在HomePage.php上? 我在我的代码中使用了$_SESSION但由于某种原因它没有将它们链接在一起。 任何帮助将不胜感激!

food.php代码:

<?php 


session_start();



if (isset($_POST['submit'])){

  $foodchoice = $_POST['meal'];


}




error_reporting( E_ALL & ~E_NOTICE );


if (isset($foodchoice)) 
{

$foodchoice = $_POST['meal'];

$_POST['meal'] = $_SESSION['meal'];

}

?>

homepage.php代码:

这是为了检查会话是否有效,但他们从不这样做:

if (isset($_SESSION['meal'])) {

echo " foodchoice is set";

}

else if (!isset($_SESSION['meal'])) {

echo "nay!";

}

我如何引用homepage.php中的$_SESSION数据:

<?php  

if (is_array($_SESSION['meal']) || is_object($_SESSION['meal']))
{
    foreach ($_SESSION['meal'] as $checkbox)
    {
        echo "</br>  ".$checkbox . '';
    }


}

?>

最后这是我的表单数据:

<form action="homePage.php" method="post">

 <input type='checkbox' value='The Share Collection' name = "meal[]" id='button3'>

<input type='checkbox' value='Assorted Biscuits' name = "meal[]" id='button3'>

<input type='checkbox' value='Fruit Skewers' name = "meal[]" id='button3'>

<input type='checkbox' value='Bread Project' name = "meal[]" id='button3'>
 <input type='checkbox' value='Cheese Project' name = "meal[]" id='button3'>

<input type="submit" id="button4" value="Submit Your Selection" name="submit">

2 个答案:

答案 0 :(得分:1)

在foodchoice.php中,您没有为$ _SESSION [&#39; meal&#39;]分配任何值。你有它倒退的地方:

$_POST['meal'] = $_SESSION['meal'];

改为 $_SESSION['meal'] = $_POST['meal'];$_SESSION['meal'] = $foodchoice;

不确定为什么要重复$ foodchoice = $ _POST [&#39;饭&#39;];在你的第二个条件内,除非我遗漏了什么。

答案 1 :(得分:0)

你的food.php看起来应该是这样的:

<?php 

session_start();
error_reporting( E_ALL & ~E_NOTICE );


if (isset($_POST['submit'])){

    $foodchoice = $_POST['meal'];

}

if (isset($foodchoice)) 
{
    $_SESSION['meal'] = $_POST['meal'];
}