I have this form with some dynamic fields called num_entradas.
If you add some numeric values onchange calculates the sum of all the fields with the function sumarTotalEntradas():
All of these works fine, but the problem is when I submit the form to a PHP page I only get the last field value. If I change the name attribute of the fields to num_entradas[] the submit is ok but then the sumaTotalEntradas() function doesn't work and totalEntradas fields remains in 0.
function sumarTotalEntradas() {
var arr = document.getElementsByName('num_entradas');
var tot = 0;
var subtotal = 0;
for (var i = 0; i < arr.length; i++) {
if (parseInt(arr[i].value))
tot = tot + subtotal;
}
document.getElementById('totalEntradas').value = tot;
}
<form>
<input type="number" min="0" step="1" maxlength="2" name="num_entradas" id="num_entradas" value="0" onchange="sumarTotalEntradas()">
<input type="number" min="0" step="1" maxlength="2" name="num_entradas" id="num_entradas" value="0" onchange="sumarTotalEntradas()">
<input type="number" min="0" step="1" maxlength="2" name="num_entradas" id="num_entradas" value="0" onchange="sumarTotalEntradas()">
Total: <input type="text" maxlength="2" name="totalEntradas" id="totalEntradas" value="0"> €
</form>