我正在建立一个新的在线比萨订购网站,并希望在Checkoutpage.php上打印出用户的订单,包括价格。如何使用会话来实现这一目标?
我尝试使用会话,php,获取和发布。
OnlinePizzaOrderingPage.php
<!DOCTYPE html>
<html>
<head>
<title>Online Pizza Ordering Page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>Online Pizza Ordering Page</h1>
<a href="Detailedsauceandquantitypage.php?pizza=supreme">
<img src="supreme.jpg" width="82" height="86" title="Supreme"
alt="Supreme">
</a>
<a href="Detailedsauceandquantitypage.php?pizza=meatlover">
<img src="meatlover.jpg" width="82" height="86"
title="Meatlover" alt="Meatlover">
</a>
<a href="Detailedsauceandquantitypage.php?pizza=hawaii">
<img src="hawaii.jpg" width="82" height="86" title="Hawaii"
alt="Hawaii">
</a>
<a href="Detailedsauceandquantitypage.php?pizza=fourseasons">
<img src="fourseasons.jpg" width="82" height="86" title="Four
Seasons" alt="Four Seasons">
</a>
<a href="Detailedsauceandquantitypage.php?pizza=vege">
<img src="vege.jpg" width="82" height="86" title="Vege"
alt="Vege">
</a>
</body>
</html>
sauceandquantitypage.php详细信息
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
<title>Detailed sauce and quantity page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>Detailed sauce and quantity page</h1>
<form action="ShoppingCartpage.php" method="POST">
<!-- set form value to retrieve and pass the selected pizza to
the cart -->
<input type="hidden" name="pizza" value="<?php echo
htmlspecialchars($_GET['pizza'], ENT_QUOTES, 'UTF-8', false); ?>">
<img src="bbq.jpg" alt="BBQ">
<label for="numberOfSauces">Number of Pizzas (0-100):</label>
<input type="number" name="bbqPizza" min="0" max="100"
value="0">
<br>
<img src="tomato.jpg" alt="Tomato">
<label for="numberOfSauces">Number of Pizzas (0-100):</label>
<input type="number" name="tomatoPizza" min="0" max="100"
value="0">
<br>
<img src="salsa.jpg" alt="Salsa">
<label for="numberOfSauces">Number of Pizzas (0-100):</label>
<input type="number" name="salsaPizza" min="0" max="100"
value="0">
<br>
<input type="submit" value="Add to cart" name="submit">
<br>
</form>
</body>
</html>
ShoppingCartpage.php
<?php
if (!session_id()) {
//initialize the session if it is not already
session_start();
}
if (!array_key_exists('pizzas', $_SESSION)) {
//create initial storage of all pizzas
$_SESSION['pizzas'] = [];
}
if (array_key_exists('submit', $_POST)) {
if (!array_key_exists('pizza', $_POST)) {
//a pizza was not selected, redirect the user back to the first
page.
}
//determine sauce type quantities
foreach(['bbqPizza', 'tomatoPizza', 'salsaPizza'] as $sauce) {
//retrieve the quantity as an integer value
if ($qty = filter_var($_POST[$sauce], FILTER_VALIDATE_INT)) {
//add to the order only those that were selected
//use a placeholder to store the quantity of sauce and
pizza
$key = $_POST['pizza'] . '_' . $sauce;
if (!array_key_exists($key, $_SESSION['pizzas'])) {
$_SESSION['pizzas'][$key] = [
'pizza' => $_POST['pizza'],
'sauce' => $sauce,
'qty' => 0
];
}
//increment the quantity of the pizza and sauce
$_SESSION['pizzas'][$key]['qty'] += $qty;
}
}
}
if (empty($_SESSION['pizzas'])) {
//no pizzas were added redirect to the first page.
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<ol>
<?php if (!empty($_SESSION['pizzas'])) {
foreach($_SESSION['pizzas'] as $pie) { ?>
<li>
<?php printf('%s %s pizzas with %s sauce', $pie['qty'],
$pie['pizza'], $pie['sauce']); ?>
</li>
<?php }
} ?>
</ol>
<br>
<a href="OnlinePizzaOrderingPage.html">Add more pizzas to cart</a>
<a href="Checkoutpage.html">Go to checkout</a>
</body>
</html>
Checkoutpage.php
<!DOCTYPE html>
<html>
<head>
<title>Checkout page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>Checkout page</h1>
<form action="Confirmationpage.php" method="POST">
<input type="text" name="fName" placeholder="First Name"><br>
<input type="text" name="lName" placeholder="Last Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<input type="number" name="phoneNumber" placeholder="Phone Number">
<br>
<input type="submit" value="Confirm" name="submit">
</form>
</body>
</html>
预期结果是在Checkoutpage.php上显示所有用户的订单及其各自的价格,然后显示总价。实际结果为不显示任何内容。