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>
This is my truly basic, almost style-less, pure-PHP calculator form:
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 theeval()
process with BOMDAS in mind:Code: (Demo)