Sum of fields for input

0

Good morning,

I have the following:

<div class="form-group col-md-2">
  <label for="campo4">Valor da Peça</label>
  <input type="text" class="form-control" name="customer['valor_venda']">
</div>

. .            Order Value             . .            Order Value             . . Among other inputs, but these I need to add to insert in a field, where I tried

 <div class="form-group col-md-2">
  <label for="campo6">Valor Total da Venda</label>
  <input ('%.2n', $customer['valor_venda']+$customer['valor_venda2']+$customer['valor_venda3']+$customer['valor_venda4']+$customer['valor_venda5'] . "\n") class="form-control" name="customer['total']" >
  </div>

I have tried in many ways, including, stupidly, like this:

  <div class="form-group col-md-2">
  <label for="campo6">Valor Total da Venda</label>
  <input type="text" class="form-control" name="customer['total']" value="<?php echo money_format ('%.2n', $customer['valor_venda']+$customer['valor_venda2']+$customer['valor_venda3']+$customer['valor_venda4']+$customer['valor_venda5'] . "\n"); ?>">
</div>

I'm not able to write the 'total' value in BD (MySQL) and I'm not sure what to do.

Some light, please !!

    
asked by anonymous 16.02.2017 / 12:06

2 answers

0

This is an idea:)

function calcular() {
    var num1 = Number(document.getElementById("num1").value);
    var num2 = Number(document.getElementById("num2").value);
    var num3 = Number(document.getElementById("num3").value);
    var num4 = Number(document.getElementById("num4").value);
    var num5 = Number(document.getElementById("num5").value);
    document.getElementById("total").value = parseFloat(num1 + num2 + num3 + num4 + num5).toFixed(2);

   }
<form method="post" action="">
<div class="form-group col-md-2">
  <label for="campo4">Valor da Peça</label>
<input type="text" class="form-control" name="customer['valor_venda']" id="num1" onblur="calcular();" />
<input type="text" class="form-control" name="customer['valor_venda2']" id="num2" onblur="calcular();" />
<input type="text" class="form-control" name="customer['valor_venda3']" id="num3" onblur="calcular();" />
<input type="text" class="form-control" name="customer['valor_venda4']" id="num4" onblur="calcular();" />
<input type="text" class="form-control" name="customer['valor_venda5']" id="num5" onblur="calcular();" />
</div>
<div class="form-group col-md-2">
  <label for="campo6">Valor Total da Venda</label>
<input type="text" id="total" class="form-control" name="total" >
<input type="submit" value="submit"> 
</form>
    
16.02.2017 / 23:53
0

You can loop the variable $customer and add each value.

$soma = 0;
foreach($customer as $valor){
   $soma = $soma + $valor;
}

This code will go through all the data in your Array and add everything to the $soma variable. I hope I have helped!

    
16.02.2017 / 16:15