Process on the server and return in a PHP Input

Asked

Viewed 96 times

-3

Hello, I’m making a calculator on PHP and HTML, and I wanted that when the user pressed in Ubmit, be calculated on the server, and the result returned in the result Input, if you did not understand, I will try to explain in a more basic way:

User Adds the numbers in the first and second text field (Input) the result will be calculated and will appear in third field.

I wanted to do it using PHP, thank you in advance.

  • What’s the question? Have a problem with specific code?

  • Yeah, I wish I knew how to do that

  • 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

  • I’m learning PHP, my teacher hasn’t explained it to me yet, but if it’s that easy, I’ll research

  • 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

  • But could you tell me which command I would use?

  • 1

    What you want to do is you understand perfectly, what you can’t understand is what part you’re having the trouble with.

Show 2 more comments

1 answer

0


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 !

  • Very good! But I could put the $result inside an HTML input?

  • Oláa, Siim, only using HMTL and PHP there is a way, for example using the same file, so you send the result to the page itself, I will post an example :)

  • 1

    Okay, I’m on hold! Thank you very much

  • 2

    @Karenvicente I recommend giving a read in this publication about the images

  • @Karenvicente was very helpful! But an error persists, in the input result, appears: <?= $total ? >, i.e., the code, not the result :( What can I do?

  • @Virtual detector could you send your complete code? Have you tried it like this?: <? php echo $total; ?>

  • Here is the code https://pastebin.com/ePBaj6SS

  • @Virtual Detector Hello!! good I think the problem is in <select>, you must pass an attribute named "value" for each option Ex: <option value="Add">+</option>, so just use the attribute "name" to get the selected value $_POST["select"];:

  • Okay, I’ll change that

  • Unfortunately, the error persists :(

  • 1

    But all right, although I would love to do it that way, I did it another way, thanks anyway!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.