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

0

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

I tried to do but it is giving error and I do not know how to solve it because I am a 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;
    }
}

? >

    
asked by anonymous 19.05.2017 / 16:33

3 answers

0

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

Here's a simple example with no 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 Number.class.php

<?php
class Numero {

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

        return $soma;
    }   
}
    
19.05.2017 / 18:09
0

I have done 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 do the calculation.

form.php

<!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;
    }

}

?> 
    
20.05.2017 / 03:09
0

For what you posted it looks like it will use two pages.

One for the form and one for the PHP page.

If this is the case, then put in the action of form ../model/numeros.php

Otherwise, it will use a single file, place the HTML and PHP on the same page as the one 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>";
}
    
19.05.2017 / 19:12