doujiao8649 2016-01-12 23:30
浏览 37
已采纳

我的PHP收银计算器程序不起作用[重复]

It's supposed to look like this: sample output. It will only say "Price must be a valid number" and nothing else when I test it, even when my inputs are correct numbers, it still won't calculate. I don't know what I am doing wrong here.

Here is the index.php

    <?php 
        //set default value of variables for initial page load
        if (!isset($description)) { $description = ''; } 
        if (!isset($unit_price)) { $unit_price = ''; } 
        if (!isset($quantity)) { $quantity = ''; } 
    ?> 
    <!DOCTYPE html>
    <html>
    <head>
        <title>Assignment 1</title>
        <link rel="stylesheet" type="text/css" href="YapAss1.css">
    </head>

    <body>
        <main>
        <h1>Cashier</h1>
        <?php if (!empty($error_message)) { ?>
            <p class="error"><?php echo htmlspecialchars($error_message); ?>               </p>
        <?php } ?>
        <form action="checkout.php" method="post">

            <div id="data">
                <label>Description:</label>
                <input type="text" name="investment"
                       value="<?php echo htmlspecialchars($description); ?>">
                <br>

                <label>Unit Price:</label>
                <input type="text" name="interest_rate"
                       value="<?php echo htmlspecialchars($unit_price); ?>">
                <br>

                <label>Quantity:</label>
                <input type="text" name="years"
                       value="<?php echo htmlspecialchars($quantity); ?>">
                <br>
            </div>

            <div id="buttons">
                <label>&nbsp;</label>
                <input type="submit" value="Checkout Now"><br>
            </div>

        </form>
        </main>
    </body>
    </html>

checkout.php

    <?php
        // get the data from the form
        $description = filter_input(INPUT_POST, 'description',
            FILTER_VALIDATE_FLOAT);
        $unit_price = filter_input(INPUT_POST, 'unit_price',
            FILTER_VALIDATE_FLOAT);
        $quantity = filter_input(INPUT_POST, 'quantity',
            FILTER_VALIDATE_INT);


       if ( $unit_price === FALSE )  {
            $error_message = 'Price must be a valid number.'; 
        } else if ( $unit_price <= 0 ) {
            $error_message = 'Price must be a valid number.'; 

        // validate quantity
        } else if ( $quantity === FALSE ) {
            $error_message = 'Quantity must be a valid number.';
        } else if ( $quantity <= 0 ) {
            $error_message = 'Quantity must be a valid number.';
        } else {
            $error_message = ''; 
        }


        // if an error message exists, go to the index page
        if ($error_message != '') {
            include('index.php');
            exit(); 
        }

         // calculate the future value
        $sales_tax = .07;
        $sub_total = $price * $quantity;
        $total = $subtotal * $sales_tax; 



        // apply currency and percent formatting

        $unit_price_f = '$'.number_format($unit_price, 2);
        $quantity_f = '$'.number_format($quantity, 2);
        $sub_total_f ='$'.number_format($sub_total, 2);
        $sales_tax_f = $sales_tax.'%';
        $total_f ='$'.number_format($total, 2);
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Assignment 1</title>
        <link rel="stylesheet" type="text/css" href="YapAss1.css">
    </head>
    <body>
        <main>
            <h1>Checkout 12/27/2015</h1>

            <label>Item Description:</label>
            <span><?php echo $description; ?></span><br>

            <label>Price:</label>
            <span><?php echo $unit_price_f; ?></span><br>

            <label>Quantity:</label>
            <span><?php echo $quantity_f; ?></span><br>

            <label>Sub Total:</label>
            <span><?php echo $sub_total_f; ?></span><br>

            <label>Sales Tax:</label>
            <span><?php echo $sales_tax; ?></span><br>

            <label>Total:</label>
            <span><?php echo $total_f; ?></span><br>
        </main>
    </body>
    </html>
</div>
  • 写回答

2条回答 默认 最新

  • duanheye7423 2016-01-12 23:46
    关注

    Here is the fault:

     $description = filter_input(INPUT_POST, 'description',
                FILTER_VALIDATE_FLOAT);
            $unit_price = filter_input(INPUT_POST, 'unit_price',
                FILTER_VALIDATE_FLOAT);
            $quantity = filter_input(INPUT_POST, 'quantity',
                FILTER_VALIDATE_INT);
    

    You have incorrect input names submitted here.

    New Code

     $description = filter_input(INPUT_POST, $_POST['investment'],
                FILTER_VALIDATE_FLOAT);
            $unit_price = filter_input(INPUT_POST, 'interest_rate',
                FILTER_VALIDATE_FLOAT);
            $quantity = filter_input(INPUT_POST, 'years',
                FILTER_VALIDATE_INT);
    

    You also have some undefined variables in your code. You need to recheck all the variables you are using.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度