我正在建立一个网站订购披萨,并希望能够在结帐页面上输出用户的订单。我需要知道用户单击的图像,每个图像都是不同的披萨。这种调料效果很好,因为我可以使用带有提交按钮的表单,但是对于OnlinePizzaOrderingPage.html,我不能使用提交按钮。我已在下面添加了sql标记,因为允许使用sql,但是我不知道它在这种情况下是否会有所帮助。如何实现在ShoppingCart.php页面上显示用户选择的比萨饼类型?
我已经尝试过将POST和GET与php和Javascript一起使用。
OnlinePizzaOrderingPage.html
<!DOCTYPE html>
<html>
<head>
<title>Online Pizza Ordering Page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script>
function imageTitle(title){
alert(title);
console.log(title);
}
</script>
</head>
<body>
<h1>Online Pizza Ordering Page</h1>
<a href="Detailedsauceandquantitypage.html"><img src="supreme.jpg"
width="82" height="86" title="Supreme" alt="Supreme"
onclick="imageTitle(this.title);"></a>
<a href="Detailedsauceandquantitypage.html"><img src="meatlover.jpg"
width="82" height="86" title="Meatlover" alt="Meatlover"
onclick="imageTitle(this.title);"></a>
<a href="Detailedsauceandquantitypage.html"><img src="hawaii.jpg"
width="82" height="86" title="Hawaii" alt="Hawaii"
onclick="imageTitle(this.title);"></a>
<a href="Detailedsauceandquantitypage.html"><img
src="fourseasons.jpg"
width="82" height="86" title="Four Seasons" alt="Four Seasons"
onclick="imageTitle(this.title);"></a>
<a href="Detailedsauceandquantitypage.html"><img src="vege.jpg"
width="82"
height="86" title="Vege" alt="Vege"
onclick="imageTitle(this.title);"></a>
</body>
</html>
DetailedSauceandquantitypage.html
<!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">
<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
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script>
function imageTitle(title){
alert(title);
console.log(title);
}
</script>
</head>
<body>
<h1>Shopping Cart page</h1>
<?php
// define variables and set to empty values
$bbqPizza = $tomatoPizza = $salsaPizza = "";
if ( isset( $_POST['submit'] ) ) {
$bbq = $_POST["bbqPizza"];
$tomato = $_POST["tomatoPizza"];
$salsa = $_POST["salsaPizza"];
echo $bbq . " pizzas with bbq sauce, " . $tomato . " pizzas with
tomato sauce and " . $salsa . " pizzas with salsa sauce.";
}
?>
<br>
<a href="OnlinePizzaOrderingPage.html">Add more pizzas to cart</a>
<a href="Checkoutpage.html">Go to checkout</a>
</body>
</html>
预期结果是用户通过单击OnlinePizzaOrderingPage.html上要显示在ShoppingCart.php页面上的图像选择的披萨类型。
答案 0 :(得分:2)
.html
没有检索或存储可传递到PHP文件的请求值(发布/获取)的本地方法。您需要将DetailedSauceandquantitypage.html
更改为PHP页面,并在 OnlinePizzaOrderingPage.html 中使用查询字符串来确定单击了哪个链接,例如href="DetailedSauceandquantitypage.php?pizza=Supreme"
。
正如您在上一个问题中提到的那样,您应该认真 考虑将所有
.html
文件转换为.php
,以避免 以后要进行大量更改时,当您要扩展自己的 应用程序文件以使用php代码。
有关更多详细信息,请参见Variables From External Sources
如果您使用的是Apache Web服务器,则可以使用SSI
(server side includes),但是当您使用的是PHP时,将其转换为PHP会更加简单,而且出错的可能性也较小。
示例:
为简洁起见,我删除了一些JavaScript。除了将其存储在数据库中之外,我还不确定您希望如何添加或签出当前订单。
OnlinePizzaOrderingPage.html
<!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>
详细的调味料和量页面 .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
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart page</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>Shopping Cart page</h1>
<?php
// define variables and set to empty values
$bbqPizza = $tomatoPizza = $salsaPizza = "";
if ( isset( $_POST['submit'] ) ) {
$pizza = $_POST['pizza']; //value from first page
$bbq = $_POST["bbqPizza"];
$tomato = $_POST["tomatoPizza"];
$salsa = $_POST["salsaPizza"];
echo "A " . $pizza . " pizza, " . $bbq . " pizzas with bbq sauce, " . $tomato . " pizzas with tomato sauce and " . $salsa . " pizzas with salsa sauce.";
//where are these stored to Add More or Checkout?
}
?>
<br>
<a href="OnlinePizzaOrderingPage.html">Add more pizzas to cart</a>
<a href="Checkoutpage.html">Go to checkout</a>
</body>
</html>