【问题标题】:My PHP cashier calculator program won't work [duplicate]我的 PHP 出纳计算器程序无法工作 [重复]
【发布时间】:2016-01-12 23:30:04
【问题描述】:

它应该看起来像这样:sample output。 当我测试它时,它只会说“价格必须是一个有效的数字”,而不是别的,即使我的输入是正确的数字,它仍然不会计算。我不知道我在这里做错了什么。

这里是 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>

【问题讨论】:

  • 您要验证的字段与表单中的字段名称没有相似之处:years !== quantity; interest_rate !== unit_price; investment !== description
  • 天哪。我回顾了很多次,但仍然忽略了这一点。非常感谢

标签: php mysql apache xampp


【解决方案1】:

这是错误:

 $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);

您在此处提交的输入名称不正确。

新代码

 $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);

您的代码中还有一些未定义的变量。您需要重新检查您正在使用的所有变量。

【讨论】:

    【解决方案2】:

    在 checkout.php 页面上有 if 语句:

    if ( $unit_price === FALSE ) 
    

    所以你正在检查一个布尔值,而这个值必须是一个整数。 我只是猜测您的目标是检查是否设置了此变量。 通过将 if 语句更改为:

    if (!isset(($unit_price))  
    

    变量 $quantity 也是如此。 通过进行此调整,您可以删除 index.php 前 6 行中的 php。 祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多