How to put the value of a PHP variable inside an input?

4

How to put the value of a variable (already computed in PHP) within an input ?

<form method="post" action="calculos.php">
<div id="circulantes">
    <div class="ativocirculante" id="ativocirculante">
        <h2>ATIVO CIRCULANTE<input type="text" placeholder="R$ 0,00" id="ac" readonly/></h2>
        <h4>Ativo Errático (Financeiro) <input type="text" placeholder="R$ 0,00 aplicações" id="ae"/></h4>
        <h4>Disponíveis (Caixa e Bancos)<input type="text" placeholder="R$ 0,00" id="disp"/></h4>
        <h3>ACO<input type="text" placeholder="R$ 0,00" id="aco" readonly/></h3>
        <h4>Contas a receber<input type="text" placeholder="R$ 0,00" id="cr" name="ncr" value="<?php echo $crTot;?>" readonly/></h4>
        <h4 id="cr1">Até 30 dias<input type="text" placeholder="R$ 0,00" id="cr11" name="ncr11"/></h4>
        <h4 id="cr2">31 a 60 dias<input type="text" placeholder="R$ 0,00" id="cr22" name="ncr22"/></h4>
        <h4 id="cr3">61 a 90 dias<input type="text" placeholder="R$ 0,00" id="cr33" name="ncr33"/></h4>
        <h4 id="cr4">Acima de 90 dias<input type="text" placeholder="R$ 0,00" id="cr44" name="ncr44"/></h4>
        <h4>Estoque<input type="text" placeholder="R$ 0,00" id="est"/></h4>
        <h4>Adiantamento a Fornecedores<input type="text" placeholder="R$ 0,00" id="af"/></h4>
        <h4>Despesas antecipadas<input type="text" placeholder="R$ 0,00" id="da"/></h4>
        <input type="submit" value="calcular">
    </div>


<?php
$crTot = "";
if(isset($_POST)){
    $cr1 = $_POST['ncr11'];
    $cr2 = $_POST['ncr22'];
    $cr3 = $_POST['ncr33'];
    $cr4 = $_POST['ncr44'];
    $crTot = $cr1+$cr2+$cr3+$cr4;
    echo $crTot;
}
    
asked by anonymous 02.10.2017 / 13:54

3 answers

6

Use the "value = value"

Ex:

<input type="text" placeholder="R$ 0,00" id="cr33" name="ncr33" value=<?php echo $variavel ?>/>

Do this:

<?php
$crTot = "";
if(isset($_POST)){
    $cr1 = $_POST['ncr11'];
    $cr2 = $_POST['ncr22'];
    $cr3 = $_POST['ncr33'];
    $cr4 = $_POST['ncr44'];
    $crTot = $cr1+$cr2+$cr3+$cr4;
    echo $crTot;
}
?>
<form method="post" action="calculos.php">
<div id="circulantes">
.....
restante do código
    
02.10.2017 / 13:58
5

You're doing it right, but in the wrong order. What happens is that you did not declare the $crTot variable before attempting to use it. To fix, just invert the order of php with HTML :

<?php
$crTot = ""; //Aqui ele está criando a variável, a partir daqui, ela pode ser usada.
if(isset($_POST)){
    $cr1 = $_POST['ncr11'];
    $cr2 = $_POST['ncr22'];
    $cr3 = $_POST['ncr33'];
    $cr4 = $_POST['ncr44'];
    $crTot = $cr1+$cr2+$cr3+$cr4;
    echo $crTot;
}
?>
<form method="post" action="calculos.php">
<div id="circulantes">
    <div class="ativocirculante" id="ativocirculante">
        <h2>ATIVO CIRCULANTE<input type="text" placeholder="R$ 0,00" id="ac" readonly/></h2>
        <h4>Ativo Errático (Financeiro) <input type="text" placeholder="R$ 0,00 aplicações" id="ae"/></h4>
        <h4>Disponíveis (Caixa e Bancos)<input type="text" placeholder="R$ 0,00" id="disp"/></h4>
        <h3>ACO<input type="text" placeholder="R$ 0,00" id="aco" readonly/></h3>
        <h4>Contas a receber<input type="text" placeholder="R$ 0,00" id="cr" name="ncr" value="<?php echo $crTot; ?>" readonly/></h4>
        <h4 id="cr1">Até 30 dias<input type="text" placeholder="R$ 0,00" id="cr11" name="ncr11"/></h4>
        <h4 id="cr2">31 a 60 dias<input type="text" placeholder="R$ 0,00" id="cr22" name="ncr22"/></h4>
        <h4 id="cr3">61 a 90 dias<input type="text" placeholder="R$ 0,00" id="cr33" name="ncr33"/></h4>
        <h4 id="cr4">Acima de 90 dias<input type="text" placeholder="R$ 0,00" id="cr44" name="ncr44"/></h4>
        <h4>Estoque<input type="text" placeholder="R$ 0,00" id="est"/></h4>
        <h4>Adiantamento a Fornecedores<input type="text" placeholder="R$ 0,00" id="af"/></h4>
        <h4>Despesas antecipadas<input type="text" placeholder="R$ 0,00" id="da"/></h4>
        <input type="submit" value="calcular">
    </div>
</div>
    
02.10.2017 / 14:09
1

Use isset also in the variable $crTot and identify the variable in the post as for example $_POST['ncr11'] , I made the following code for help:

<form method="post" action="calculos.php">
    <div id="circulantes">
        <div class="ativocirculante" id="ativocirculante">
            <h2>ATIVO CIRCULANTE<input type="text" placeholder="R$ 0,00" id="ac" readonly/></h2>
            <h4>Ativo Errático (Financeiro) <input type="text" placeholder="R$ 0,00 aplicações" id="ae"/></h4>
            <h4>Disponíveis (Caixa e Bancos)<input type="text" placeholder="R$ 0,00" id="disp"/></h4>
            <h3>ACO<input type="text" placeholder="R$ 0,00" id="aco" readonly/></h3>
            <h4>Contas a receber<input type="text" placeholder="R$ 0,00" id="cr" name="ncr" value="<?php if(isset($crTot)){ echo $crTot; } ?>" readonly/></h4>
            <h4 id="cr1">Até 30 dias<input type="text" placeholder="R$ 0,00" id="cr11" name="ncr11"/></h4>
            <h4 id="cr2">31 a 60 dias<input type="text" placeholder="R$ 0,00" id="cr22" name="ncr22"/></h4>
            <h4 id="cr3">61 a 90 dias<input type="text" placeholder="R$ 0,00" id="cr33" name="ncr33"/></h4>
            <h4 id="cr4">Acima de 90 dias<input type="text" placeholder="R$ 0,00" id="cr44" name="ncr44"/></h4>
            <h4>Estoque<input type="text" placeholder="R$ 0,00" id="est"/></h4>
            <h4>Adiantamento a Fornecedores<input type="text" placeholder="R$ 0,00" id="af"/></h4>
            <h4>Despesas antecipadas<input type="text" placeholder="R$ 0,00" id="da"/></h4>
            <input type="submit" value="calcular">
        </div>


    <?php
    $crTot = "";
    if(isset($_POST['ncr11'])){
        $cr1 = $_POST['ncr11'];
        $cr2 = $_POST['ncr22'];
        $cr3 = $_POST['ncr33'];
        $cr4 = $_POST['ncr44'];
        $crTot = $cr1+$cr2+$cr3+$cr4;
        echo $crTot;
    }
    ?>
</form>

I made this code considering that this page is called calculos.php .

    
02.10.2017 / 14:07