[英]How to output user input in a different webpage?
我正在建立一個網站訂購披薩,並希望能夠在結帳頁面上輸出用戶的訂單。 我需要知道用戶單擊的圖像,每個圖像都是不同的披薩。 這種調料效果很好,因為我可以使用帶有提交按鈕的表單,但是對於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>
詳細的Sauceandquantitypage.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頁面上的圖像選擇的比薩餅類型。
.html
沒有檢索或存儲可傳遞到PHP文件的請求值(發布/獲取)的本地方法。 您需要將DetailedSauceandquantitypage.html
更改為PHP頁面,並在OnlinePizzaOrderingPage.html中使用查詢字符串來確定單擊了哪個鏈接,例如href="DetailedSauceandquantitypage.php?pizza=Supreme"
。
正如您在上一個問題中提到的那樣,當您想要擴展應用程序文件以使用php代碼時,應認真考慮將所有
.html
文件轉換為.php
,以避免以后進行大量更改。
有關更多詳細信息,請參見來自外部源的變量
如果您使用的是Apache Web服務器,則可以使用SSI
(服務器端包含) ,但是當您使用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>
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.