How to present the result of a form on the same page?

2

I have a form that gives me the result on another page, but I would like the result to appear on the same page.

 <form name="calc" method="get" enctype="multipart/form-data" action="resultado.php"> <input type="text" id="Peso" name="peso" required="">
        <input type="text" id="Altura" name="altura" required="">
        <input type="text" id="Peso" name="peso" required="">
        <input type="submit" class="button-green" value="CALCULAR">
</form>

resultado.php

<div id="resultado">
        <?php 
$peso = $_GET['peso'];
$altura = $_GET['altura'];


$conta1 = $altura*$altura;
$conta2 = $peso/$conta1;


$resultado = number_format($conta2);


if(isset($resultado) && $resultado != '0'){; 
echo '<h1>Seu IMC é:</h1>';
echo '<h2>'.$resultado.'</h2>';
}else{
echo '<h1>Por favor, utilize apenas numeros!</h1>'; 
}
?>
        </div>
    
asked by anonymous 08.12.2015 / 17:42

3 answers

8

You need:

  • Add the result.php code in the same form file.
  • Leave action empty or put # in form.
  • Check if there is any value in $_GET , use empty () for this.

A simple example:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL|E_STRICT);

if(!empty($_GET) && $_SERVER['REQUEST_METHOD'] == 'GET'){
    $nome = $_GET['nome'];
    echo 'valor recebido: '. $nome;
}   
?>

<form action="#">
    <input type="text" name="nome" />
    <input type="submit" />
</form>
    
08.12.2015 / 17:59
4

You can use the PHP_SELF method, but you need to change some more details in your code:

1 - I used the post method, (but you can use GET ) and checked with isset if the $_POST variable was not empty, otherwise, as @rray said, already the first time you open the page will appear the result considering the fields with 0 and 0.

2 - From what I understand, you have one more field in your HTML (twice the weight field).

3 - Formatting the output with number format requires 3 parameters, and you were not passing any.

Below is the normalized code with what I think is the behavior you expect:

<form name="calc" method="post" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'] ?>">
    <label for="Peso"> Peso
    <input type="text" id="Peso" name="peso" required="">
    </label>
    <label for="Altura"> Altura
    <input type="text" id="Altura" name="altura" required="">
    </label>
    <input type="submit" class="button-green" value="CALCULAR">
</form>

<div id="resultado">
    <?php
    if (isset ($_POST)) {
        $peso = isset($_POST['peso']) ? $_POST['peso'] : false;
        $altura = isset($_POST['altura']) ? $_POST['altura'] : false;
        if (isset($peso) && $peso > 0 && isset($altura) && $altura > 0) {
            $conta1 = $altura * $altura;
            $conta2 = $peso / $conta1;
            $resultado = number_format($conta2, 2, ".", ",");

            var_dump($altura);
            var_dump($peso);

            if (isset($resultado) && $resultado != '0') {
                ;
                echo '<h1>Seu IMC é:</h1>';
                echo '<h2>' . $resultado . '</h2>';
            }
            if (isset($resultado) && $resultado < 17) {
                echo 'é menor que 17';
            } else {
                echo '<h1>Por favor, utilize apenas numeros!</h1>';
            }
        }
    }
    ?>
</div>

See Ideone's IMC for a skinny guy . :)

    
08.12.2015 / 17:56
1

Try:

<?php

if ( count( $_GET ) && isset( $_GET['peso'] ) && isset( $_GET['peso'] ) ) { ?>
    // Digite o código do cálculo do resultado aqui
    Fazer novo cálculo
<?php } else { ?>
    // Digite o código do formulário aqui
}
    
09.12.2015 / 19:39