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

Asked

Viewed 3,914 times

5

How to put the value of a variable (already calculated in PHP) inside a 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;
}

3 answers

6


Use the "value=value"

Ex.:

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

Do so:

<?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
  • But it’s already been done, so what’s the problem?

  • The following message appears inside the input: (<br /><b>Notice</b>: Undefined variable: crTot in <b>C: xampp htdocs menuVertical apCirculantes.php</b> on line <b>24</b><br />)

  • This means that the variable ! You set the variable before trying to print ?

  • Add PHP code before form

  • Yes, I believe it is defined in the code below: <?php $crTot = ""; if(isset($_POST)){ $cr1 = $_POST['ncr11']; $cr2 = $_POST['ncr22']; $Cr3 = $_POST['ncr33']; $cr4 = $_POST['ncr44']; $cr1+$cr2+$Cr3+$$cr4; echo $crTot; }

  • Put the PHP code before the form, and don’t forget to close: <? php $crTot = ""; if(isset($_POST)){.... } ?>

  • the PHP code is added on a page called "calculos.php" and my action is calling this function.

  • Yes but these definitions $cr1 = $_POST['ncr11'], etc have to stay before your form

  • Look at the addition I made

  • Okay, I put it before the form and now the "Undefined variable" message is missing, but she’s not calculating the sum of the others.

  • this input (<H4>Accounts receivable<input type="text" placeholder="R$ 0,00" id="cr" name="ncr" value="<?php echo $crTot;?>" readonly/></H4>) should appear within it the value of the sum of the others, correct? If yes, there is some error because it is null...

  • There is problem of how it is bringing by the POST. To test you can do so: $cr1 = 1; $cr2 = 2; $Cr3 = 3; $cr4 = 4;

  • It worked Raoni BZ, but why is not rolling with the POST?

  • Then it would be a problem of how you are sending and searching with the POST. .

  • Okay! I will. Thank you

  • Guys, I’m sorry but I’m a beginner on this site. How do I finish a question?

  • I do not know if it is still so, but I never opened questions. https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply

  • There’s a " V " below the arrows next to the answer, there’s no ?!

  • OK, finished and created another for the POST.

Show 14 more comments

5

You’re doing it right, but in the wrong order. What happens is that you haven’t declared the variable $crTot before trying to use it. To fix, just reverse the order of the php with the 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>
  • Hi Francisco, in this case the page returns this: Notice: Undefined index: ncr11 in C: xampp htdocs menuVertical apCirculantes.php on line 20 Notice: Undefined index: ncr22 in C: xampp htdocs menuVertical apCirculantes.php on line 21 Notice: Undefined index: ncr33 in C: xampp htdocs menuVertical apCirculantes.php on line 22 Notice: Undefined index: ncr44 in C: xampp htdocs menuVertical apCirculantes.php on line 23 0

1

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

<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 taking into account that this page is called calculos.php.

  • This way solves his problem, because the error is because it does not find the variable $crTot, and to avoid some Warning put to check the $_POST['ncr11'].

Browser other questions tagged

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