PHP-要求用户输入 - 计算

时间:2013-12-26 01:31:55

标签: php

我一直试图让这个简单的程序工作,但出于某种原因,我没有得到任何结果。任何建议将不胜感激。谢谢。

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    Price of the Meal
    <input value="100" type="text" name="meal"><br>
    Tip
    <input value="20" type="text" name="tip">
    <input type="submit" name="submit">


    <?php
    $meal = $_POST['meal'];
    $tip = $_POST['tip'];

    $total_amount = check($meal, 10, $tip);

    function check($meal, $tax, $tip) {

        $tax_amount = $meal * .10;

        $tip_amount = $meal * ($tip / 100);

        $total_amount = $meal + $tax_amount + $tip_amount;
        echo "Price of the meal " . $meal . "\n";


        echo "tax_amount " . $tax_amount . "\n";
        echo "tip_amount " . $tip_amount . "\n";

        return $total_amount;
    }

    echo "Total " . $total_amount;
    ?>

1 个答案:

答案 0 :(得分:0)

代码正在进行少许更改

$tax_amount = $meal * ($tax/100);

并添加条件

 if (isset($_POST['meal']) && isset($_POST['tip']) && $_POST['meal'] > 0 && $_POST['tip'] > 0) {
  echo "Price of the meal " . $meal . "\n";
  echo "tax_amount " . $tax_amount . "\n";
  echo "tip_amount " . $tip_amount . "\n";
}


if (isset($_POST['meal']) && isset($_POST['tip']) && $_POST['meal'] > 0 && $_POST['tip'] > 0)
  echo "Total " . $total_amount;