繁体   English   中英

如何在其他网页上输出用户输入?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM