dqab0824 2017-01-02 17:05
浏览 42
已采纳

php简单的开关案例计算器功能不显示结果

I am trying to create a simple php calculator with swich case statement. I used bootstrap buttons to each +,-,*,/ functions. At the end when user fill the form and press any of these buttons it is supposed to display the results in a disabled text field. But it won't display the results

<!DOCTYPE html>
<html>
<head>
<title>SIMPLE CALCULATOR</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>

<div class="container-fluid mainbox row">
    <h2 style="text-align: center;">Calculator</h2>
    <div class="container-fluid calbox col-md-4 offset-md-4">
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div class="form-group">
            <label style="padding-top: 10px;">Value 1</label>
            <input type="text" class="form-control" placeholder="Value 1" name="val1" id="val1">
        </div>
        <div class="form-group">
            <label>Value 2</label>
            <input type="text" class="form-control" placeholder="Value 2" name="val2" id="val2">
        </div>
        <div class="form-group row" style="padding-left: 10px;">
            <button type="submit" class="btn btn-outline-primary" name="btn" value="add">+</button>
            <button type="submit" class="btn btn-outline-primary" name="btn" value="sub">-</button>
            <button type="submit" class="btn btn-outline-primary" name="btn" value="mul">*</button>
            <button type="submit" class="btn btn-outline-primary" name="btn" value="div">/</button>
        </div>
        <div class="form-group">
            <label>Result</label>
            <input type="text" class="form-control" name="res" value="<?php cal() ?>" disabled>
        </div>
    </form>
    </div>
</div>

        <?php
        function cal(){
            if (isset($_POST['submit'])) {
                $val1 = $_POST['val1'];
                $val2 = $_POST['val2'];

                $func = $_POST['btn'];

                switch ($func) {
                    case "add":
                    $compute = $val1 + $val2;
                    break;

                    case "sub":
                    $compute = $val1 - $val2;
                    break;

                    case "mul":
                    $compute = $val1 * $val2;
                    break;

                    case "div":
                    $compute = $val1 / $val2;
                    break;

                    default:
                    $compute = "error";
                    break;
                }
                return $compute;
            }
        }
    ?>

<script type="text/javascript" src="js/jquery311.min.js"></script>
<script type="text/javascript" src="js/tether.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

Give some tips to determine the problem

  • 写回答

3条回答 默认 最新

  • dongzhi7763 2017-01-02 17:40
    关注

    I was able to get your code to work by doing these two modifications:

    • <?php cal() ?> to <?php echo cal(); ?> Without this you may compute some result, but you never actually output it.

    • isset($_POST['submit']) to isset($_POST['btn']) Without that you never compute a result. You do not define an input element with name "submit", but you do define one with the name "btn".


    Apart from that two general remarks:

    • you should move that calculation function to a separate file and include it at the beginning of your file. That keeps separate things separate, logic and presentation.
    • you should consider doing such computations on the client side, that saves you from doing a full page reload for each calculation.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码