欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php简单计算器实现

程序员文章站 2022-04-24 18:14:43
php简单计算器实现

php简单计算器实现

<?php
header("content-type:text/html;charset=utf-8");
//error_reporting( E_ALL&~E_NOTICE );
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2017/8/24
 * Time: 21:03
 */
?>

<?php
$num1 = isset($_GET['num1'])?$_GET['num1']:'';
$num2 = isset($_GET['num2'])?$_GET['num2']:'';
$symbol = isset($_GET['symbol'])?$_GET['symbol']:'';
$result = "";
    switch ($symbol){
        case "+":
            $result = $num1 + $num2;
            break;
        case "-":
            $result = $num1 - $num2;
            break;
        case "*":
            $result = $num1 * $num2;
            break;
        case "/":
            $result = $num1 / $num2;
            break;
    }
?>
<!--简单计算器-->
<form action="" method="get" onsubmit="return function() {}" id="form1">
    <script src="js/jquery.min.js"></script>
    <input type="text" name="num1" id="num1" placeholder="请输入数字" value="<?php echo $num1;?>">
    <select name="symbol" id="symbol">
        <option value="+" <?php if($symbol == "+"){ echo "selected";}?>>+</option>
        <option value="-" <?php if($symbol == "-"){ echo "selected";}?>>-</option>
        <option value="*" <?php if($symbol == "*"){ echo "selected";}?>>*</option>
        <option value="/" <?php if($symbol == "/"){ echo "selected";}?>>/</option>
    </select>
    <input type="text" name="num2" id="num2" placeholder="请输入数字" value="<?php echo $num2;?>">
    <input type="button" value="计算" id="btn">
    <input type="text" name="result" value="<?php echo $result;?>">
</form>
<a href="ji-suan-qi.php">刷新</a>
<script>
    $("#btn").click(function () {
        var num2 = Number($("#num2").val());
        var symbol = $("#symbol").val();
//        alert(1);
        if(symbol == "/"){
            if(num2 == 0){
                alert("除数不能为0");
                return false;
            }
        }

        $("#form1").submit();
    })
</script>