PHP result on the same screen

Asked

Viewed 785 times

-1

I need the result of the form to be presented on the same screen, I made the following code:

<?php
//Recolhe os valores digitados no formulário
$v = $_POST['valor'];
$tm = $_POST['txmensal'];
$p = $_POST['periodo'];

$ta = (((($tm/100)+1)**12)-1)*100;
?>

Here ... the form

        <form class="form form-control" method="POST">
        <h5 class="mt-2">Converter juros mensais para anuais</h5>
            <label for="valor" name="valor">Valor </label>
                <input type="text" name="valor" class="form-control">

            <label for="txmensal" name="txmensal" class="mt-1">Taxa Mensal </label>
                <input type="text" name="txmensal" class="form-control" placeholder="%">

            <label for="periodo" name="periodo" class="mt-1">Período </label>
                <input type="text" name="periodo" class="form-control" placeholder="meses">

            <input class="btn btn-default mt-1" type="submit" value="Submit">   

            <?php
                if ($_POST['submit']){?>
                     <div class="alert alert-success mt-4 text-center">
                         <?php echo 'A taxa anual é: ' .$ta?>
                    </div>
            <?php }?>
        </form>

inserir a descrição da imagem aqui

  • Post the code in text not images, post also the form that passes the data to php

  • See if it’s clearer, I tried to simplify the code even more, just by checking if the fomulario was sent

  • change if($_POST["submit"]) for if(isset($_POST["submit"])) isset() serves to know if a variable exists or not (returns a boolean), but from what I see I believe that an application with Javascript would be simpler fast or provide a better experience for the user

  • It returns the :error of the img

3 answers

2


As mentioned by Guilherme, in the comments of the question, use the function isset, it will check whether the value exists or not, for example:

<?php

/* Se existir o índice `submit` em $_POST, então faça o cálculo */
if (isset($_POST['txmensal'])) {
    $v = $_POST['valor'];
    $tm = $_POST['txmensal'];
    $p = $_POST['periodo'];

    $ta = (((($tm/100)+1)**12)-1)*100;
}
/* Caso contrário, atribua NULL para à variável */
else {
    $ta = null;
}
?>

And to display, you can use the following code

<?php
    /* Exiba o resultado caso a variável possua um valor diferente de null */
    if ($ta !== null) { ?>
        <div class="alert alert-success mt-4 text-center">
            <?php echo 'A taxa anual é: ' .$ta?>
        </div>
<?php } ?>

Complete code:

<?php

/* Se existir o índice `submit` em $_POST, então faça o cálculo */
if (isset($_POST['txmensal'])) {
    $v = $_POST['valor'];
    $tm = $_POST['txmensal'];
    $p = $_POST['periodo'];

    $ta = (((($tm/100)+1)**12)-1)*100;
}
/* Caso contrário, atribua NULL para à variável */
else {
    $ta = null;
}
?>
<form class="form form-control" method="POST">
    <h5 class="mt-2">Converter juros mensais para anuais</h5>

    <label for="valor" name="valor">Valor </label>
    <input type="text" name="valor" class="form-control">

    <label for="txmensal" name="txmensal" class="mt-1">Taxa Mensal </label>
    <input type="text" name="txmensal" class="form-control" placeholder="%">

    <label for="periodo" name="periodo" class="mt-1">Período </label>
    <input type="text" name="periodo" class="form-control" placeholder="meses">

    <input class="btn btn-default mt-1" type="submit" value="Submit">   

    <?php
        /* Exiba o resultado caso a variável possua um valor diferente de null */
        if ($ta !== null): ?>
            <div class="alert alert-success mt-4 text-center">
                <?php echo 'A taxa anual é: ' .$ta?>
            </div>
    <?php endif ?>
</form>

The attribute action, when you want to send the data to the same page, it becomes optional.

  • This one almost completely solves ! But it does not appear the result, when I click on Submit, nothing happens

  • @Flávia I edited my answer, added the full code.

  • Valdeir! Perfect ! From what I saw. the problem was the "endif" right... that worked perfectly! Valew

  • @Flavia Both do the same thing. Probably something was not changed during the realization of the explanation, it happens. rsrs

0

Do you want to $ta is displayed on the screen? If so, it is correct to use the command echo, the return just returns the value to be used in some function or something of the kind, displays nothing.

Just change the return for echo.

-1

Do it like this bro:

    <?php

    if(isset($_POST['valor']) && isset($_POST['txmensal']) && isset($_POST['periodo']) ){
        $v = $_POST['valor'];
        $tm = $_POST['txmensal'];
        $p = $_POST['periodo'];

        $_POST['ta'] = (((($tm/100)+1)**12)-1)*100;

    }
    ?>


    <form class="form form-control" method="POST">
    <h5 class="mt-2">Converter juros mensais para anuais</h5>
        <label for="valor" name="valor">Valor </label>
            <input type="text" name="valor" class="form-control">

        <label for="txmensal" name="txmensal" class="mt-1">Taxa Mensal </label>
            <input type="text" name="txmensal" class="form-control" placeholder="%">

        <label for="periodo" name="periodo" class="mt-1">Período </label>
            <input type="text" name="periodo" class="form-control" placeholder="meses">

        <input class="btn btn-default mt-1" type="submit" value="Submit">   

        <?php
            if (isset($_POST)){
                 echo "<div class='alert alert-success mt-4 text-center'>A taxa anual é: " . $_POST['ta'] . "</div>";
            }
        ?>
    </form>

Browser other questions tagged

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