I think a very simple way to do this would be (if you don’t know the language very well yet), use the shaman with apache to run the files.
If you use xampp, after it’s installed, just add your files and projects in the C: xampp htdocs directory and "start" apache.
Well come on.
Create an html file that will have your form, for example:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Calculadora</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="calcular.php">
Primeiro número:<br>
<input name="primeiro" type="text" value="" size="30"/><br>
Segundo número:<br>
<input name="segundo" type="text" value="" size="30"/><br>
Operacao:<br>
<select name="operacao">
<option value="+">Somar</option>
<option value="-">Subtrair</option>
<option value="/">Dividir</option>
<option value="*">Multiplicar</option>
</select><br>
<input type="submit" value="Calcular"/>
</form>
</body>
</html>
Well the "action" attribute of the form is pointing to another file, in case a file called calculate.php is in it that will be made all the logic of the calculator.
<?php
$primeiro = $_POST['primeiro'];
$segundo = $_POST['segundo'];
$operacao = $_POST['operacao'];
$resultado = 0;
if ($operacao == "+") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "-") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "*") {
$resultado = $primeiro * $segundo;
} else if ($operacao == "/") {
$resultado = $primeiro / $segundo;
}
echo $resultado;
?>
To add the result to an input on the same screen only using HTML, PHP, a solution would be to create only one file. php, for example calculate.php and in it you will have both the logic of the calculator and the html form, would look like this:
<?php
$primeiro = $_POST['primeiro'];
$segundo= $_POST['segundo'];
$operacao = $_POST['operacao'];
$resultado = 0;
if ($operacao == "+") {
$resultado = $primeiro + $segundo;
} else if ($operacao == "-") {
$resultado = $primeiro - $segundo;
} else if ($operacao == "*") {
$resultado = $primeiro * $segundo;
} else if ($operacao == "/") {
$resultado = $primeiro / $segundo;
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Calculadora</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="">
Primeiro número:<br>
<input name="primeiro" type="text" value="<?= $primeiro ?>" size="30"/><br>
Segundo número:<br>
<input name="segundo" type="text" value="<?= $segundo ?>" size="30"/><br>
Operacao:<br>
<select name="operacao">
<option value="+">Somar</option>
<option value="-">Subtrair</option>
<option value="/">Dividir</option>
<option value="*">Multiplicar</option>
</select><br>
Resultado:<br>
<input name="resultado" type="text" value="<?= $resultado ?>" size="30"/><br>
<input type="submit" value="Calcular"/>
</form>
</body>
</html>
But it is worth remembering that there are other solutions such as using ajax, js etc..
If you are only at the beginning of the world of programming I advise you to go through the simple and go learning step by step things, as you are feeling more comfortable you can try other solutions.
But what was changed in this example compared to the first?
Come on, let’s go,
I removed the value of the "action" attribute from the form, because it will point to the same page, that is, it will be redirected by clicking "Calculate" to calular.php again, but as you will have filled the data, it is possible to pick up via POST or GET. And that way I can display the result through the tags in the HTML form.
I hope I’ve helped !
What’s the question? Have a problem with specific code?
– Denis Rudnei de Souza
Yeah, I wish I knew how to do that
– DetetiveVirtual
It is a relatively simple task, have you tried something? Is there a problem in a code you have already developed or is it a question of how to do it? In the second case, I recommend reviewing the knowledge you have about the language, when you are better used, then you try to do something
– Denis Rudnei de Souza
I’m learning PHP, my teacher hasn’t explained it to me yet, but if it’s that easy, I’ll research
– DetetiveVirtual
Just a reminder: It’s easy if you already have a language base, if you don’t know anything, recommend learning a little more and then try to do
– Denis Rudnei de Souza
But could you tell me which command I would use?
– DetetiveVirtual
What you want to do is you understand perfectly, what you can’t understand is what part you’re having the trouble with.
– Bacco