How to receive form data and inform the result of the sum in php in the form div element?

Asked

Viewed 582 times

0

I need to do this: Create an HTML form with two text fields a div for presentation of the results and a button of type Submit. 1 - Create a PHP script, which receives form data and informs in the div element of form the result of the sum of the numbers

I tried to do but is giving error and I do not know solve because I am beginner in programming

HELP!!!!!

<body>
<div>
    <form action="../model/numeros.php" method="POST">

    Informe um número
  <input type="text" placeholder="Número" name="n1"><br>
    Informe outro número
  <input type="text" placeholder="Número" name="n2"><br>
  <input type="submit"></input><br>

    <b>Resultado da soma:</b>
</div>
<div>
    <?php
        require_once '../model/numeros.php';
        $o = new numeros();
        $dados=$o->Somar();
        echo $dados;
    ?>
</div>

My php class:

<?php
//$n1=$_GET['n1'];
//$n2=$_GET['n2'];
class numeros{
    $n1=$_POST["n1"];
    $n2=$_POST["n2"];
    public function Somar()
    {

        $total=$n1+$n2;
        return $total;
    }
}

?>

  • what is the error that is giving? take the action from the form

3 answers

0

I did using 2 pages, one for the form (html) that will receive the data and print the result, and another for the script (php), which will make the calculation.

php form.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Formulário</title>
    </head>
    <body>
        <div>
        <form method="post" action="">
            <br>Número 1  <input type="text" placeholder="digite aqui..." name="t1"><br><br>
            Número 2  <input type="text" placeholder="digite aqui..." name="t2">
            <br><br>
            <input type="submit" value="Somar">
        </form><br>
        </div>
        <div><br>
            <?php

                if(isset($_POST['t1'])&&(isset($_POST['t2']))){

                    $n=$_POST["t1"];
                    $n1= $_POST["t2"];

                    require_once 'soma.php';
                    $res = new Soma();
                    $soma=$res->somar($n, $n1);

                    echo "A soma é: ".$soma;
                }
            ?>

        </div>
    </body>
</html>

Soma.php

<?php
class Soma{
    function somar($n,$n1){

        $res =$n+$n1;
        return $res;
    }

}

?> 
  • Remembering that an alternative way to call the function isset for each variable is you pass multiple variables to the function: if (isset($_POST['t1'], $_POST['t2']))

0

You can not put the class file in the action, you have to put the script that will call the class Numbers, in this case you will call the file itself, and then yes take the values of $_POST and pass to the sum function.

Follow a simple example without any validation:

index.php file

<?php
    include_once 'Numero.class.php';
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <title>Exemplo</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <form action="" method="post">
        <p>
            <label>Informe o primeiro número</label><br/>      
            <input type="text" placeholder="Primeiro número" name="primeiroNumero" value="">
        </p>

        <p>
            <label>Informe o segundo número</label><br/>
            <input type="text" placeholder="Segundo número" name="segundoNumero" value=""><br>
        </p>

        <input type="submit"></input><br>
    </form>
    <?php
        $post = filter_input_array(INPUT_POST);

        if (isset($post)) {

            $primeiroNumero = $post["primeiroNumero"];
            $segundoNumero = $post["segundoNumero"];

            $numero = new Numero();
            $soma = $numero->somar($primeiroNumero, $segundoNumero);
    ?>
            <br/>

            <div class="resultado-soma">
                O resultado da soma é: <?=$soma?>
            </div>
    <?      
        }
    ?>
</body>
</html>

File Numero.class.php

<?php
class Numero {

    public function somar($primeiroNumero, $segundoNumero) {
        $soma = $primeiroNumero + $segundoNumero;

        return $soma;
    }   
}
  • I don’t think it makes much sense for you to apply isset on the return of function filter_input_array. Regardless of the input, the variable will always be defined and this will not guarantee that the indexes used within the if actually exist.

0

From what you posted it looks like you’ll use two pages.

One for the form and one for the PHP page.

If that’s the case, then put it in the form action ../model/numeros.php

Otherwise, you will use a single file, put HTML and PHP on the same page as below.

HTML

<form action="" method="POST">
  Informe um número
  <input type="text" placeholder="Número" name="n1"><br>
  Informe outro número
  <input type="text" placeholder="Número" name="n2"><br>
<input type="submit"></input>

PHP

if (isset($_POST['n1'], $_POST['n2'])){
     echo "<b>Resultado da soma:</b><br><div>". ($_POST["n1"] + $_POST["n2"]) . "</div>";
}
  • Remembering that if ($_POST['n1']) is not a way to check if the index exists in the global variable. The most interesting is to use the function isset even: if (isset($_POST['n1'], $_POST['n2']))

  • I just wanted to save a few bytes, :)

Browser other questions tagged

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