Basically I store the history in a hidden input(Hidden).
The idea is to send your php script the updated value of the history field in a post variable. Once it arrives in your script it is concatenated with the string that represents the current operation.
Well, initially it is empty but after the first execution it starts to accumulate the history contained in the input:
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
So the value inside the input is being updated:
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
I only added this block inside your if:
if(isset($valor2) && isset($valor1) && isset($operador)){
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
echo $historico;
echo "</br>";
}
It checks whether the operations variables are set and adds the variable $histórico
the value of the hidden input:
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
This field has the function of sending your php script the history to be concatenated with '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
Note that I use isset so there are no empty variable alerts when they are not set.
Complete code
<?php
if ( isset($_POST['calcularbtn']) ) {
$valor1 = $_POST['valor1'];
$valor2 = $_POST['valor2'];
$tipo = $_POST['tipo'];
if ($tipo == 'Somar') {
$operador = '+';
$resultado = $valor1 + $valor2;
}elseif ($tipo == 'Subtrair') {
$operador = '-';
$resultado = $valor1 - $valor2;
}elseif ($tipo == 'Multiplicar') {
$operador = '*';
$resultado = $valor1 * $valor2;
}elseif ($tipo == 'Dividir') {
$operador = '/';
$resultado = $valor1 / $valor2;
}elseif ($tipo == 'Potência') {
$resultado = pow($valor1, $valor2);
}else{
$resultado = pow($valor1, 1/2);
}
echo 'Resultado=' . $resultado;
}else{
echo "Esperando cálculo...";
}
if(isset($valor2) && isset($valor1) && isset($operador)){
$historico = $_POST['historico'] . '</br>' . $valor1 . $operador . $valor2 . '=' . $resultado ;
}
?>
<form class="form-signin" method="post" action="#">
<div class="form-label-group">
<input hidden="" type="text" name="historico" value="<?php if (isset($historico)) { echo $historico; } ?>" >
<input class="form-control" type="number" name="valor1" size="5" placeholder="Valor 1"><br>
<select class="custom-select d-block w-100" name="tipo" >
<option selected="selected">Somar</option>
<option>Subtrair</option>
<option>Multiplicar</option>
<option>Dividir</option>
<option>Potência</option>
<option>Raiz Quadrada</option>
</select><br><br>
</div>
<div class="form-label-group">
<input class="form-control" type="number" name="valor2" size="5" placeholder="Valor 2"><br>
</div>
<input class="btn btn-outline-secondary" type="submit" name="calcularbtn" value="Calcular">
</form>`
<?php
if(isset($historico)){
echo $historico;
echo "</br>";
}
?>
Completion: There are a billion ways to do this. But there are more appropriate ways. This is not the most appropriate way to build a calculator using php just as building it using php is not appropriate.
But for educational purposes the use of hidden fields are quite useful to traffic data from the client to the server without the user having control over them.
Tip:
Use javascript
with it you can leave the dynamic process and use more resources. Think: why ask the server something your browser already knows ?.
Ask the browser about javascript and it will know very well what to do with your data. Besides of course, not having to reload the same page to get the result.
What’s missing? Which way out?
– Francisco
The history is missing, every calculation I do needs to appear the account on the same page, like the calculator in windows 10 and 8.I will edit with the picture of the calculator.
– Lone Tonberry
But what’s the exit from what’s been done so far?
– Francisco
The saaida is the result of the operation with the " echo $result " and the account made with " echo $_SESSION['historico']['valor1'] ." $operator ". $_SESSION['historico']['valor2'] ." = ". $result ; "
– Lone Tonberry
The idea is that every time I press the calculate button it shows the result of the account and adds from the page all accounts made without deleting the first account made.
– Lone Tonberry
You can create a history.txt file
– Andrei Coelho
And what I do in this file ?
– Lone Tonberry
@Lonetonberry gives a look
– Andrei Coelho