I have a form with four numeric fields, ncr11
, ncr22
, ncr33
and ncr44
. There is still another field in the form, ncr
, which will receive a total value, which is the sum of all four first fields. The sum I did with PHP and I was informed in another question that I should keep the action
of the form blank and I run the code on the same page, however, when I refresh the page, my result field always ends up going blank.
What I would like to do is that when the user entered the values, the result field would be updated automatically.
<?php
$crTot = "";
if($_POST){
$cr1 = $_POST['ncr11'];
$cr2 = $_POST['ncr22'];
$cr3 = $_POST['ncr33'];
$cr4 = $_POST['ncr44'];
$crTot = $cr1+$cr2+$cr3+$cr4;
echo $crTot;
}
?>
<form method="post" action="">
<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>
</div>