【问题标题】:php gratuity calculator errorphp小费计算器错误
【发布时间】:2015-01-24 05:48:47
【问题描述】:

我正在尝试用 php 制作一个小费计算器,当我输入账单金额和小费百分比时,将返回总金额的结果;

我希望它与账单金额:、小费金额和总金额一起显示在表格上。

但是在我的代码工作时遇到问题,它总是给我一个错误,说找不到对象!,这是我到目前为止的代码。

 <html>
<body>
    <h2>Tip Calculator</h2>
    <form action="res.php" method="post">
        <table border="0">
        <tr>
            <td>Bill Amount:</td>
            <td><input type="text" name="bill" value="0" /></td>
        </tr><tr>
            <td>Tip Percent:</td>
            <td><input type="text" name="tip_percent" value="0"/></td>
        </tr><tr>
        <td></td>
            <td><input type="submit" name="calculate" value="Calculate" /></td>
    </tr>
        </table>
    </form>
</body>
</html>
<?php
if(isset($_POST['bill'], $_POST['tip_percent'])){
if(is_numeric($_POST'bill']) && is_numeric($_POST['tip_percent'])){
        echo "Bill: ".$_POST['bill'];
        if($_POST['tip_percent']<0) 
            $_POST['tip_percent'] = 0; 
        echo "<br/>Tip %: ".$_POST['tip_percent'];
        echo "<br/>Tip Amount: ".$tip = number_format($_POST['bill'] * $_POST['tip_percent'] / 100, 2);
        echo "<br/>Total Amount: ".$total = $tip+$_POST['bill'];
    }else echo "At least one of the values was not a number!";
}else echo "At least one of the values was missing!";
?>

【问题讨论】:

  • 请说明您遇到了什么错误
  • '它总是给我一个错误' - 那你为什么不在这里发布呢?
  • 您使用的是method="post",但您尝试将字段读取为$_GET,因此您将收到未定义索引错误。
  • 我看到我将其更改为 POST,但仍然找不到对象!错误
  • @852saber 抱歉,哥们,如果您需要进一步的帮助,请使用您收到的完整错误消息更新您的问题并提供尽可能多的信息,帮助您了解代码中清晰可见的内容。

标签: php html


【解决方案1】:

尝试修复语法错误:

if(is_numeric($_POST'bill'])
                    ^

与:

if(is_numeric($_POST['bill'])

'bill'] 开头缺少方括号

【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2022-11-10
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多