如何修复PHP Post Request中没有回显的变量?

时间:2019-05-31 05:34:56

标签: php html

我正在建立一个网站,并希望能够获得其要订购的披萨数量的客户编号输入,并将其发送到ShoppingCartpage.php

我已经尝试过使我的文件成为php文件。我已经尝试过使用POST。 我现在面临的问题是无法将订购的比萨饼数量输出到ShoppingCart.php页面。

Detailedsauceandquantitypage.html

<!DOCTYPE html>
    <html>
        <head>
            <script src="script.js"></script>
            <title>Detailed sauce and quantity page</title>
        </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">
                <input type="submit" value="Add this sauce to pizza">

                <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">
                <input type="submit" value="Add this sauce to pizza">

                <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">
                <input type="submit" value="Add this sauce to pizza">

                <br>
            </form>
        </body>
    </html>

ShoppingCartpage.php

<!DOCTYPE html>
<html>
    <head>
        <title>Shopping Cart page</title>
    </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 . " " . $tomato . $salsa . "\n"; die("Please 
        work!");
        }
        ?>
        <a href="OnlinePizzaOrderingPage.html">Add more pizzas to cart</a>
        <a href="Checkoutpage.html">Go to checkout</a>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

您应该像这样添加输入按钮的名称。

<input type="submit" value="Add this sauce to pizza" name="submit">

POST将读取输入的名称而不是类型,因此您需要添加名称。

答案 1 :(得分:0)

1。仅需要一个表单中的提交按钮(在表单</form>前的末尾)

<!DOCTYPE html>
    <html>
        <head>
            <script src="script.js"></script>
            <title>Detailed sauce and quantity page</title>
        </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">

                <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">

                <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">

                <br>
                <input type="submit" name="submit" value="Add this sauce to pizza">
            </form>
        </body>
    </html>

2。向按钮添加name属性。

3。您必须稍微修改PHP代码

<!DOCTYPE html>
<html>
    <head>
        <title>Shopping Cart page</title>
    </head>
    <body>
        <h1>Shopping Cart page</h1>
        <?php
            $bbq    = ( !empty($_POST["bbqPizza"]) ) ? $_POST["bbqPizza"] : 0;
            $tomato = ( !empty($_POST["tomatoPizza"]) ) ? $_POST["tomatoPizza"] : 0;
            $salsa  = ( !empty($_POST["salsaPizza"]) ) ? $_POST["salsaPizza"] : 0;
            echo $bbq . " " . $tomato . $salsa . "\n"; die("Please work!");

        ?>
        <a href="OnlinePizzaOrderingPage.html">Add more pizzas to cart</a>
        <a href="Checkoutpage.html">Go to checkout</a>
    </body>
</html>

注意:-始终尝试检查POST数据而不是submit按钮