PHP result on the same screen

-1

I need the result of the form to appear 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>

    
asked by anonymous 22.03.2018 / 15:47

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

Full 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 action attribute, when you want to send the data to the same page, it becomes optional.

    
22.03.2018 / 16:29
0

Do you want $ta to be displayed on the screen? If yes, the correct one is to use the command echo , the return only returns the value to be used in some function or something of the sort, it does not display anything.

Just change return to echo .

    
22.03.2018 / 15:58
-1

So do hand:

    <?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>
    
22.03.2018 / 16:40