Sum of fields for input

Asked

Viewed 98 times

0

Good morning,

I got 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>

. . Value of the Piece . . Value of the Piece . . Among other inputs, but these I need to add to insert in a field, in which 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’ve tried many ways, including, dumb, 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 am unable to write the 'total' value in the BD (Mysql) and I am without ideas what to do.

Some light, if you please !!

  • Milestones do you want, when filling in the input value, the calculation to be done automatically, this? Or are you searching from PHP the values just want to add them and display in input?

  • and why a . "\n"at the end of the sum?

  • Yes, that the calculation is done automatically and I can write to the BD’s'total'. With respect to . "| "as I commented: "even, stupidly..." rsrsrs

2 answers

0

You can loop the variable $customer and add up 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 in the variable $soma. I hope I’ve helped!

  • Or do $soma += $valor;

  • Good! That way is better even!

  • The point is that I have to add 5 fields: sale, sale value2, sale value3, sale value4 value5 to record in total. .

  • <div class="form-group col-Md-6"> <label for="campo2">Product 3</label> <input type="text" class="form-control" name="Customer['producto3']"> </div> <div class="form-group col-Md-2"> <label for="campo3">Serial</label> <input type="text" class="form-control" name="Customer['serial3']"> </div> <div class="form-group col-Md-2"> <label for="field4">Part value</label> <input type="text" class="form-control" name="Customer['valor_venda3']" id="num3" onkeyup ="calculate();" /> </div> <</div>

0


That’s 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>

  • Instead of the onblur event (which is triggered when the input loses focus) you can use the onkeyup event for the calculation to take place at typing time.

  • Unsuccessful with onblur and onkeyup .

  • Then the error must be in your php or sql because here tested and is writing to the server database

  • My error: So it worked -- <input type="text" id="total" class="form-control" name="Customer['total']">

  • Grateful for the great help !!!

Browser other questions tagged

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