Only PHP Code Calculator with Clickable Buttons as

2020-03-30 18:35发布

问题:

I am trying to code a calculator with only PHP and HTML code.

The Calculator should look like a real one and the buttons should be clickable.

Right now, I am stuck at combining 2 numbers, this means I press one button the number will be shown. But after I press the second button the first value disappears because of the type="submit".

My Question is: How can I save the value and show all pressed buttons together?

I know I can do that with a hidden input, but I don't know how to use them properly.

Also how am I able to calculate the whole expression after doing that?

I know javascript or any other client-side language would be better, but I want to find a way to do it purely with PHP and HTML.

<!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:

This is my truly basic, almost style-less, pure-PHP calculator form:

<!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",(sizeof($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>

Here is a screenshot while I was testing:

You can see that I've made every button a submit button with the same name, but different values. I use a single hidden input to preserve built expression. There will be many enhancements and considerations beyond this demo, it is up to you how far to go down this rabbit hole.


P.S. For anyone who just unholstered their sidearm, squinted one eye, and sternly uttered "Hey, we don't take kindly to folks like you callin' eval() in these here parts!". Well, I've endeavored to adequately validate the string to be evaluated. If there is a known hole, please let me know and I'll try to fix it. Alternatively, this is my attempt to re-invent the eval() process with BOMDAS in mind:

Code: (Demo)

$stored="2+3*4/6";
$components=preg_split('~([*/+-])~',$stored,NULL,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