php 制作计算器-点击按钮时,文本框中显示否,

n53p2ov0  于 2022-12-25  发布在  PHP
关注(0)|答案(4)|浏览(110)

对了,我现在正在学习php,在你们说要用javascript代替之前,我正在学习php,想用这个代码。
基本上,我想在Windows7上复制计算器,但只是用按钮0-9,+ - / * 和=。
在你阅读代码之前,我到目前为止只构建了它,试图让它像这样工作。
你点击1,然后它会显示在文本框上,如果你点击5,它会把5后1在文本框等...然后当你点击一个运算符,如除(这是唯一一个我试图编码到目前为止,没有继续作为不工作)文本框删除数字,但保存总数,然后任何新的数字投入将被除以当提交按钮按下。
然而,当我运行我的代码,并点击任何按钮,在我的网页浏览器没有进入文本框。任何帮助将真的很感激。(学习PHP,使我可以从第一线的支持进展到网页开发在同一家公司,所以这是很重要的)
代码如下,我一直在使用xampp来运行它

<html>
<head>
<title>Calculator</title>
<style>
#wrapper{
width:500px;
height:500px;
margin:auto;
border:1px solid;
}
#content{
width:500px;
height:500px;
}
</style>
</head>


<body>
<div id="wrapper">
    <div id="content">
        <?php
            $answer = $display = $saveddisplay = $newdisplay = $add = $subtract = $divide = $multiply = $num0 = $num1 = $num2 = $num3 = $num4 = $num5 = $num6 = $num7 = $num8 = $num9 =  "";

            if($_SERVER["REQUEST_METHOD"] == "POST"){
                $num0 = ($_POST["number0"]);
                $num1 = ($_POST["number1"]);
                $num2 = ($_POST["number2"]);
                $num3 = ($_POST["number3"]);
                $num4 = ($_POST["number4"]);
                $num5 = ($_POST["number5"]);
                $num6 = ($_POST["number6"]);
                $num7 = ($_POST["number7"]);
                $num8 = ($_POST["number8"]);
                $num9 = ($_POST["number9"]);
                $add = ($_POST["add"]);
                $subtract = ($_POST["subtract"]);
                $divide = ($_POST["divide"]);
                $multiply = ($_POST["multiply"]);


                ///////////////////////////////////////////NUMBERS
                if($_POST["number0"]){
                    $display = $display && ($_POST["number0"]);
                }
                if($_POST["number1"]){
                    $display = $display && ($_POST["number1"]);
                }
                if($_POST["number2"]){
                    $display = $display && ($_POST["number2"]);
                }
                if($_POST["number3"]){
                    $display = $display && ($_POST["number3"]);
                }
                if($_POST["number4"]){
                    $display = $display && ($_POST["number4"]);
                }
                if($_POST["number5"]){
                    $display = $display && ($_POST["number5"]);
                }
                if($_POST["number6"]){
                    $display = $display && ($_POST["number6"]);
                }
                if($_POST["number7"]){
                    $display = $display && ($_POST["number7"]);
                }
                if($_POST["number8"]){
                    $display = $display && ($_POST["number8"]);
                }
                if($_POST["number9"]){
                    $display = $display && ($_POST["number9"]);
                }

                //////////////////////////////////////////OPERATORS
                if($_POST["divide"]){
                    $saveddisplay = $display;
                    $display = "";
                    if($_POST["number0"]){
                        $newdisplay = $newdisplay && ($_POST["number0"]);
                            if($_POST["submit"]){
                                $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                            }


                    }
                    if($_POST["number1"]){
                        $newdisplay = $newdisplay && ($_POST["number1"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number2"]){
                        $newdisplay = $newdisplay && ($_POST["number2"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number3"]){
                        $newdisplay = $newdisplay && ($_POST["number3"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number4"]){
                        $newdisplay = $newdisplay && ($_POST["number4"]);
                            if($_POST["submit"]){
                                $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                            }
                    }
                    if($_POST["number5"]){
                        $newdisplay = $newdisplay && ($_POST["number5"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number6"]){
                        $newdisplay = $newdisplay && ($_POST["number6"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number7"]){
                        $newdisplay = $newdisplay && ($_POST["number7"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number8"]){
                        $newdisplay = $newdisplay && ($_POST["number8"]);
                            if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }
                    if($_POST["number9"]){
                        $newdisplay = $newdisplay && ($_POST["number9"]);
                        if($_POST["submit"]){
                            $answer = $saveddisplay / $newdisplay && $newdisplay = "";

                        }
                    }

                }




            }


        ?>


        <form method ="POST" action="<?php  ($SERVER["PHP_SELF"]);?>">
        <input type="text" width="50" height="20" name="display" value="<?php  echo $display or $newdisplay or $answer;?>">

        <br><br><br><br><br><br>
        <button type="button" name="number7" value="7" style="width:20px; padding-left:5px; margin-left:2px;">7</button>
        <button type="button" name="number8" value="8" style="width:20px; padding-left:5px; margin-left:2px;">8</button>
        <button type="button" name="number9" value="9" style="width:20px; padding-left:5px; margin-left:2px;">9</button>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

        <button type="button" name="divide" value="/" style="width:20px; padding-left:5px; margin-left:2px;">/</button>
        <button type="button" name="multiply" value="*" style="width:20px; padding-left:5px; margin-left:2px;">*</button>
        <button type="button" name="subtract" value="-" style="width:20px; padding-left:5px; margin-left:2px;">-</button>
        <button type="button" name="add" value="+" style="width:20px; padding-left:5px; margin-left:2px;">+</button>
        <br><br>

        <button type="button" name="number4" value="4" style="width:20px; padding-left:5px; margin-left:2px;">4</button>
        <button type="button" name="number5" value="5" style="width:20px; padding-left:5px; margin-left:2px;">5</button>
        <button type="button" name="number6" value="6" style="width:20px; padding-left:5px; margin-left:2px;">6</button>

        <br><br>
        <button type="button" name="number1" value="1" style="width:20px; padding-left:5px; margin-left:2px;">1</button>
        <button type="button" name="number2" value="2" style="width:20px; padding-left:5px; margin-left:2px;">2</button>
        <button type="button" name="number3" value="3" style="width:20px; padding-left:5px; margin-left:2px;">3</button>

        <br><br>
        <button type="button" name="number0" value="0" style="width:20px; padding-left:5px; margin-left:2px;">0</button>
        <button type="submit" value="=" name="submit" style="width:46px; padding-left:5px; margin-left:2px;">=</button>
        </form>

    </div>
</div>
</body>





</html>
ee7vknir

ee7vknir1#

在PHP端,使用eval()读取数学表达式;检查此示例:

<?php
$res = 0;
$mathExpression = "((2+2) /4) *5";
eval("\$res = $mathExpression;");
echo $res;
?>

计算$mathExpression,结果在$res中。
并更改此行:

<form method ="POST" action="<?php  ($SERVER["PHP_SELF"]);?>">

为此:

<form method ="POST" action="<?php  echo $_SERVER["PHP_SELF"];?>">
gcmastyq

gcmastyq2#

按钮类型设置为button时不会自动上报数据,需要设置按钮类型为submit

<button type="submit" name="number6" value="6" style="width:20px; padding-left:5px; margin-left:2px;">6</button>

如果希望在提交带有equals按钮的表单之前将数字插入到input元素中,则必须使用JavaScript完成此操作。
给予输入元素和id。

<input id='result' type="text" width="50" height="20" name="display" value="<?php  echo $display or $newdisplay or $answer;?>">

向填充输入字段的每个按钮添加onClick处理程序。

<button type="button" name="number6" value="6" onClick="document.getElementById('result').value += this.value">6</button>

您可能需要稍微修改处理程序以获得所需的效果,但这应该会给予您有所了解。

ut6juiuv

ut6juiuv3#

你想做的事情完全是疯狂的。我建议你尝试不同的方法。PHP不适合做Windows计算器(Javascript会更好)。
您可以创建两个名为number1和number2的输入字段,然后创建四个提交按钮:加,减,除,乘。
在你的代码中,你应该首先检查你收到了什么POST数据。你已经填好所有的字段了吗?它们是数字吗?哪个按钮被点击了。

关于您的代码:

在PHP中,您不必定义变量,因此不必:第一个月
$display = $display && ($_POST["number0"]);这是没有意义的,它将求值为布尔值。$answer = $saveddisplay / $newdisplay && $newdisplay = "";
PHP不会在会话之间保存变量的状态,因此当您刚才单击按钮时,它不会知道这些变量是什么。
$display or $newdisplay or $answer PHP并不是那么聪明,你应该使用三元运算符或者把它们变成一个变量。
附言:我建议你阅读一些关于编程的书籍或者观看YT的教程,这看起来可能很容易,但是用PHP做一个计算器并不是那么简单。

PHP中的示例计算器:

<?php

$number1 = (int)($_POST['number1']);
$number2 = (int)($_POST['number2']);
$action  = $_POST['action'];

if(!isset($number1)){
        echo 'Missing first operand!';
}
else if(!isset($number2)){
        echo 'Missing second operand!';
}
else if(!empty($action)){
        if($action == "Add"){
                $result = $number1 + $number2;
        }
        else if($action == "Subtract"){
                $result = $number1 - $number2;
        }
        else if($action == "Multiply"){
                $result = $number1 * $number2;
        }
        else if($action == "Divide"){
                $result = $number2 != 0 ? $number1 / $number2 : "NaN";
        }

        if(isset($result)){
                echo "Your result is: ".$result;
        }
}
?>

<form action="" method="POST">
<input type="number" required="required" name="number1" />
<input type="number" required="required" name="number2" />
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Subtract" />
<input type="submit" name="action" value="Multiply" />
<input type="submit" name="action" value="Divide" />
</form>
yacmzcpb

yacmzcpb4#

试试这个示例代码。也许它会对你有帮助:)使用Javascript的onclick按钮,更好地接近海事组织。
它会在同一页上产生结果,你也可以链接。
使用.php扩展名保存此文件。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample Calculator</title>
<meta name="description" content="Simple Calculation Using Arithmetic Operators">
<meta name="author" content="AloNe ForeVer">
</head>
<body>
<body background="http://previews.123rf.com/images/iunewind/iunewind1403/iunewind140300097/26559245-Black-numbers-vector-background-with-copy-space-Stock-Vector.jpg">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><center><form name="calculator" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input style=" color: #0000ff ; font-size: 150%;" type="text" name="num1" placeholder="Input Number 1" >
<input style=" color: #0000ff ; font-size: 150%;" type="text" name="num2" placeholder="Input number 2" > <select style=" color: #3336FF; font-size: 150%;" name="action">
<option>+</option>
<option>-</option>
<option>/</option>
<option>*</option>
</select>
<input style=" color: #FF0000; font-size: 150%;" type="submit" name="submit" value="Calculate" ></form></center>

<center> <h1>
<?php
if(isset($_POST["submit"])){
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$action = $_POST["action"];

if ($action=="+") {
echo '<h1> <span style="color:#0000ff;">Addition</span>' . "</br>";
$add=$num1+$num2;
echo " <input type='text' value='$add' />";
}

if ($action=="-") {
echo '<h1> <span style="color:#FF0000;">Subtraction</span>' . "</br>";
$subtract=$num1-$num2;
echo "<input type='text' value='$subtract' />";
}

if ($action=="/") {
echo '<h1> <span style="color:#0000ff;">Division</span>' . "</br>";
$divide=$num1/$num2;
echo "<input type='text' value='$divide' />";
}

if ($action=="*") {
echo '<h1> <span style="color:#FF0000;">Multiplication</span>' . "</br>";
$multiply=$num1*$num2;
echo "<input type='text' value='$multiply' />";
}
}
?></center>
</body>
</html>

相关问题