基于给定输入的HTML和PHP算术计算

时间:2018-07-06 03:33:04

标签: php html

我试图获取用户输入,然后根据输入进行计算。

下面是主要的html。请帮助

<html>
    <body>
        Welcome there, <?php echo $_POST["name"]; ?>
        <br><br>
        ---- YOUR SELECTION -----
        <br><br>
    <?php
        $quantity = = echo $_POST["quantity"];
        var_dump($quantity);
        $bidprice = = echo $_POST["bidprice"];
        var_dump($bidprice);
        $total = = $quantity * $bidprice;
        var_dump($total);
    ?>
        Quantity selected: <?php echo $_POST["quantity"]; ?>
        <br>
        Bid price: <?php echo $_POST["bidprice"]; ?>
        <br> 
        Total: <?php echo $total; ?>
    </body>
</html>

下面是根据输入内容进行回复的表格,但可以获取名称,但无法执行计算。为什么?

<html>
    <body>
        <form action="welcome_get.php" method="post">
            What is your name?
            <input type="text" name="name">
            <br><br>
            Quantity:
            <input type="number" name="quantity">
            <br>
            Bid Price:
            <input type="number" name="bidprice">
            <br>
            <input type="submit">
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您在分配给变量"= ="时添加了$quantity = = $_POST["quantity"];,这在php中是不允许的,并且在给变量赋值时不需要添加"echo"

以下示例将帮助您学习PHP Sintax

<html>
        <body>
            Welcome there, <?php echo $_POST["name"]; ?>
            <br><br>
            ---- YOUR SELECTION -----
            <br><br>
        <?php
            $quantity = $_POST["quantity"];
           $bidprice = $_POST["bidprice"];
            $total = $quantity * $bidprice;
          ?>
            Quantity selected: <?php echo $_POST["quantity"]; ?>
            <br>
            Bid price: <?php echo $_POST["bidprice"]; ?>
            <br> 
            Total: <?php echo $total; ?>
        </body>
    </html>

输出:

Welcome there, Shivrudra Samshette 

---- YOUR SELECTION ----- 

Quantity selected: 2 
Bid price: 100 
Total: 200