我的PHP POST变量无法识别,无法打印或使用

I have a food page (breakfast.php) that displays meal packages. Once a user selects a package and confirms the selection the selection e.g. "Package 1" will be display in the homepage.php. My code is not grabbing the POST_ data and is resulting from my if statement as empty. Is there a reason for this?

I am new to php sorry.

Below is my code for both pages:

homepage.php

<?php 

session_start();

error_reporting( E_ALL & ~E_NOTICE );


if (!isset($_SESSION['time'])) {

header("Location: openingPage.php");

exit();

}


 $selection = $_SESSION['foodchoice'];

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

$selection = $_POST['foodchoice'];

}

else {

echo "error in your code!";
}

print_r($selection);

?>

breakfast.php

<?php

session_start();

if (!isset($_SESSION['time'])) {
    header("Location: openingPage.php");
    exit();
}

error_reporting( E_ALL & ~E_NOTICE );

if (isset($_POST['submit'])) {
    $selection = $_POST['foodchoice'];
}

$_SESSION['foodchoice'] = $selection;
?>

form data

<form method ="POST" id="myform" action="homePage.php">

<div class= "breakfast">
<u><h2>The Share Collection</h2></u>
</br>
<img src= "\images\Breakfast\breakfast1.jpg" class="funky">
</br>
</br>
<dl>
  <dd>Mini jar of housemade granola: vanilla yoghurt, fresh fruit (v) (6)</dd>
  <dd>Goat cheese, cherry tomato &amp; basil, mushroom tart (v) (8)</dd>
  <dd>Mini slider jamon serrano, fig jam, rocket, brie &amp; black pepper  (8)    </dd>
  <dd>Chef's mini sweet muffin (v) (8)</dd>
  <dd>Bowl of strawberries &amp; grapes (v) (1)</dd>
</dl>

</br>
<input type='button' value='Select This Package' id='button3'onclick="changemyButton()">
  </div>
  <hr>

<section> 
<h1 id="logo">Total Ordered</h1></section>
<hr>
</br>

<h4 id ="selection" name="foodchoice">   </h4>
 <br>
<br>
<br>
<br>
<input type="submit" id="button4" value="Submit Your Selection" name="submit">
</center>
</form>

the $_POST method only works with form or Curl, you have to do something like this.

// form.php
<form method="post" action="breakfast.php">
    <select name="foodchoice">
         <option value="package1">Package 1</option>
         <option value="package2">Package 2</option>
    </select>
    <input type="submit" value="Enviar"> 
</form>

Then in your breakfast.php :

<?php 
session_start();


if (!isset($_SESSION['time'])) {
  header("Location: openingPage.php");exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

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

} else {
    echo  'Uhhh no package selected!';exit;
}

Good Luck!

Uhmm, you must have a to submit post values.

Maybe if you put in homepage.php something like:

<form action="breakfast.php" method="post">
<select name="foodchoice">
     <option value="package1">Spaghetti</option>
     <option value="package2">Burger</option>
     <option value="package3">Pizza</option>
</select>
<input type="submit" value="Send"> 
</form>

Otherwise how breakfast.php will take the POST values?