当前位置: 首页 > 知识库问答 >
问题:

只有PHP代码计算器与可点击按钮作为输入

澹台镜
2023-03-14

我试图用PHP和HTML代码编写一个计算器。

计算器应该看起来像一个真正的,按钮应该是可点击的。

现在,我被困在组合2个数字,这意味着我按下一个按钮,数字就会显示出来。但在我按下第二个按钮后,第一个值消失,因为type=“submit”

我的问题是:如何保存值并同时显示所有按下的按钮?

我知道我可以用隐藏的输入来实现,但我不知道如何正确使用它们。

还有,我怎样才能计算出整个表达式呢?

我知道javascript或任何其他客户端语言会更好,但是我想找到一种纯粹使用PHP和超文本标记语言的方法

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Taschenrechner</title>
        <link href="Stylesheets/Stylesheets.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if(isset($_POST['zahl'])){
    $zahl1 = $_POST['zahl'];
}else{
    $zahl1 = 0;
}
?>

<form name="rechner" action="rechner.php" method="post">
    <input id="feld1" type="hidden" name="zahl1" value="">
    <input id="feld2" type="hidden" name="opera" value="">
    <input id="feld3" type="hidden" name="zahl2" value="">
    <input id="feld4" type="hidden" name="zwischerg" value=>
    <table align="center" style="width:300px; height:450px; border:solid thick black;">
        <tr>
            <td align="center">
                <input id="display" type="text" name="bildschirm" readonly="readonly" style="text-align: center; height: 50px; width: 216px;" value=<?php echo $zahl1; $op; $zahl2 ?>>
            </td>
        </tr>
        <tr>
            <td>
                <table align="center">
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="1" >
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="2">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="3">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="+">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="4">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="5">
                        </td>
                        <td >
                            <input class="taste" type="submit" name="zahl" value="6">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="-">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="7">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="8">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="9">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="*">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="clear" value="C">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="0">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value=".">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="/">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <input type="submit" name="gleich" value="=" style=" width: 215px; height: 50px;">
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

共有1个答案

昝唯
2023-03-14

这是我真正基本的、几乎没有样式的、纯PHP计算器形式:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic PHP Calculator</title>
</head>
<body>
<?php
//var_export($_POST);
//echo "<br>";
$buttons = [1,2,3,'+',4,5,6,'-',7,8,9,'*','C',0,'.','/','='];
$pressed = '';
if (isset($_POST['pressed']) && in_array($_POST['pressed'], $buttons)) {
    $pressed = $_POST['pressed'];
}
$stored = '';
if (isset($_POST['stored']) && preg_match('~^(?:[\d.]+[*/+-]?)+$~', $_POST['stored'], $out)) {
    $stored = $out[0];  
}
$display = $stored . $pressed;
//echo "$pressed & $stored & $display<br>";
if ($pressed == 'C') {
    $display = '';
} elseif ($pressed == '=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~', $stored)) {
    $display .= eval("return $stored;");
}

echo "<form action=\"\" method=\"POST\">";
    echo "<table style=\"width:300px;border:solid thick black;\">";
        echo "<tr>";
            echo "<td colspan=\"4\">$display</td>";
        echo "</tr>";
        foreach (array_chunk($buttons, 4) as $chunk) {
            echo "<tr>";
                foreach ($chunk as $button) {
                    echo "<td",(count($chunk) != 4 ? " colspan=\"4\"" : "") , "><button name=\"pressed\" value=\"$button\">$button</button></td>";
                }
            echo "</tr>";
        }
    echo "</table>";
    echo "<input type=\"hidden\" name=\"stored\" value=\"$display\">";
echo "</form>";
?>
</body>
</html>

这是我测试时的截图:

您可以看到,我已经将每个按钮设置为具有相同名称但不同值的提交按钮。我使用单个隐藏输入来保存构建的表达式。除了这个演示之外,还会有许多增强和考虑,这取决于你在这个兔子洞走多远。

附言:任何人只要不闻自己的侧臂,眯着一只眼睛,严肃地说:“嘿,我们对像你这样在这里打电话的人不友好”eval()!”。好吧,我已经尽力充分验证要评估的字符串。如果有一个已知的洞,请让我知道,我会设法修复它。或者,这是我试图重新发明eval()流程,并牢记BOMDAS:

代码:(演示)

$stored = "2+3*4/6";
$components = preg_split('~([*/+-])~', $stored, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
while (($index = array_search('*', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] * $components[$index + 1]);
}
while (($index = array_search('/', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] / $components[$index + 1]);
}
while (($index = array_search('+', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] + $components[$index + 1]);
}
while (($index = array_search('-', $components)) !== false) {
    array_splice($components, $index - 1, 3, $components[$index - 1] - $components[$index + 1]);
}
echo current($components);  // 4
 类似资料:
  • 我正在为学校开发一个简单的JavaFX计算器,我被困在一起,我被困在一起,该事件从两个文本字段,选择的单选按钮以及按下计算按钮时执行该事件的输入放在一起。 节目预览

  • 下面是我的代码,它设置了一个通知,即用户是通过zoom服务被bieng调用的。但是,如果用户不点击通知,而是直接点击应用程序图标,则无法正确捕获额外内容。我对如何从简历中的这个悬而未决的意图中提取额外的内容有点迷茫。 notificationcompat.builder mBuilder=new notificationcompat.builder(this);//构建新的通知mbuilder.s

  • 本文向大家介绍jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码,包括了jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码的使用技巧和注意事项,需要的朋友参考一下 在使用JqGrid时,Table中最后一列是操作列,在操作列中每一行都一个操作按钮,该操作按钮类似下拉菜单,如下图: 在点击Table中【操作】一列时需要弹出一个Div层,该Div层中包含一堆按钮,用

  • 本文向大家介绍Android点击按钮返回顶部实现代码,包括了Android点击按钮返回顶部实现代码的使用技巧和注意事项,需要的朋友参考一下 点击按钮返回顶部,直接上代码吧 布局文件 按钮点击事件 附带一个跳到底部 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 问题内容: 我有这个小网站,我想用请求库填写表格。问题是当填写表单数据并单击按钮时,我无法到达下一个站点(输入不起作用)。 重要的是,我无法通过某种类型的点击漫游器来做到这一点。这需要完成,这样我才能在没有图形的情况下运行。 前三个条目的名称,消息,符号是文本区域,步骤是我认为的按钮。 当我通过chrome手动发送请求时,表单数据如下所示: 姓名:约翰·约翰 讯息:XXX 签名:XXX 第1步 b