简体   繁体   English

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

[英]How to output user input in a different webpage?

I am setting up a website for ordering pizza and want to be able to output the user's orders on the checkout page. 我正在建立一个网站订购披萨,并希望能够在结帐页面上输出用户的订单。 I need to know which image the user clicked, each image is a different pizza. 我需要知道用户单击的图像,每个图像都是不同的披萨。 The sauce is working perfectly because I can use a form with a submit button, however for the OnlinePizzaOrderingPage.html I cannot use a submit button. 这种调料效果很好,因为我可以使用带有提交按钮的表单,但是对于OnlinePizzaOrderingPage.html,我不能使用提交按钮。 I have included the sql tag below because sql is allowed to be used, however I don't know if it will help in this scenario. 我已在下面添加了sql标记,因为允许使用sql,但是我不知道它在这种情况下是否会有所帮助。 How do I achieve display the type of pizza the user selected on the ShoppingCart.php page? 如何实现显示在ShoppingCart.php页面上用户选择的比萨饼的类型?

I have tried using POST and GET with php and using Javascript. 我试过将POST和GET与php一起使用,并使用Javascript。

OnlinePizzaOrderingPage.html 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 详细的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 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>

Expected results are the pizza type selected by the user through clicking the images on OnlinePizzaOrderingPage.html to be displayed on the ShoppingCart.php page. 预期结果是用户通过单击OnlinePizzaOrderingPage.html上要显示在ShoppingCart.php页面上的图像选择的比萨饼类型。

.html has no native way of retrieving or storing request values (post/get) that can then be passed to a PHP file. .html没有检索或存储可传递到PHP文件的请求值(发布/获取)的本地方法。 You would need to change DetailedSauceandquantitypage.html to to a PHP page, and use a querystring in OnlinePizzaOrderingPage.html to determine which link was clicked on, such as href="DetailedSauceandquantitypage.php?pizza=Supreme" . 您需要将DetailedSauceandquantitypage.html更改为PHP页面,并在OnlinePizzaOrderingPage.html中使用查询字符串来确定单击了哪个链接,例如href="DetailedSauceandquantitypage.php?pizza=Supreme"

As was mentioned in a previous question you made, you should seriously consider converting all of your .html files to .php , to avoid having make a ton of changes later, when you want to expand your application files to use php code. 正如您在上一个问题中提到的那样,当您想要扩展应用程序文件以使用php代码时,应认真考虑将所有.html文件转换为.php ,以避免以后进行大量更改。

For more details please see Variables From External Sources 有关更多详细信息,请参见来自外部源的变量

If you are using Apache web server you could use SSI (server side includes) , but as you are using PHP, it would be a lot simpler and less error prone to convert to PHP instead. 如果您使用的是Apache Web服务器,则可以使用SSI (服务器端包含) ,但是当您使用PHP时,将其转换为PHP会更加简单,并且出错的可能性也较小。

Example: 例:

I removed some of the javascript for brevity. 为了简洁起见,我删除了一些JavaScript。 I am also not sure how you would like to be able to add on to or checkout the current order, aside from storing it in a database. 除了将其存储在数据库中之外,我还不确定您希望如何添加或签出当前订单。

OnlinePizzaOrderingPage.html 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>

DetailedSauceandquantitypage .php 详细的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 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