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

Asked

Viewed 4,462 times

3

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>

php result.

<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>

3 answers

9

You need:

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

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>

4

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

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

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

3 - Formatting the output with the 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>

Check it out at Ideone the BMI of a skinny. :)

  • 1

    :Look how the method of your form. Remember to validate if something comes in the request, if not when accessing the first time it generates a Warning of Undefined index: x. Otherwise this is it :) +1

  • hehe Yes, I was seeing this after I read your reply... I will edit here worth +1

  • thanks friend worked out :)

  • have another question to put if with radio button we can go chat?

  • 1

    So @kaiquemix, you better come up with another question. Sopt works as a repository of questions about programming, and was designed to help other users who have the same questions. But don’t be shy, you can ask as many questions as you want, as long as they’re objective, well formatted, etc... the way you did this. :)

  • i am trying to do something like this: if (isset($result) < 17) { echo 'is less than 17'; } but nothing appears..

  • 1

    It is difficult to answer in comment, the best is to create another question, but try if (isset($resultado) && $resultado < 17) { echo 'é menor que 17'; }...

  • @gustavox as do for the result tbm be broken number because I can put 1.80 and 50.0 and the result only appears integer

  • 2

    So that’s practically another question... Anyway, I talked about it in the answer (take a look at the code)... You set the parameters of number format? The variable $resultado is like this: $resultado = number_format($conta2, 2, ".", ","); ? Important detail: At the entrance (in the field input) has to be with . and not with ,...

  • ta thus: $result = number_format($conta2);

  • 2

    So, but it has to be like this: $resultado = number_format($conta2, 2, ".", ",");.

  • I put thank you :)

Show 7 more comments

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
}

Browser other questions tagged

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