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

时间:2016-06-03 02:17:20

标签: php html

我有一个食品页面(breakfast.php),显示餐饮套餐。一旦用户选择一个包并确认选择,例如选择“包1”将显示在homepage.php中。我的代码没有抓取POST_数据,而是由于我的if语句为空。是否有一个原因?

我是php的新手。

以下是我的两个页面的代码:

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

2 个答案:

答案 0 :(得分:0)

$ _ POST 方法仅适用于form或Curl,您必须执行以下操作。

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

然后在 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;
}

祝你好运!

答案 1 :(得分:0)

嗯,你必须提交一个帖子值。

也许如果你把homepage.php放在:

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

否则breakfast.php将如何取POST值?