here is the code that i am using for the sub totals and grand totals but it gives me a strange result. pl. help
function totalprice()
{
len = document.forms["form1"]["quantity[]"].length;
for(i=0;i<len;i++){
a = document.forms["form1"]["quantity[]"][i].value;
b = document.forms["form1"]["price[]"][i].value
c = a * b
document.forms["form1"]["total[]"][i].value = c;
sum = 0;
for (b=0;b<len;b++){
var d = document.forms["form1"]["total[]"][b].value;
var sum = sum + d;
document.forms["form1"]["grandtotal"].value = sum;
}
}
}
and here is the html code
<form action="here.php" method="post" name="form1">
Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>
Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>
Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>
Grand Total: <input name="grandtotal">
<input type="submit" value="Submit">
</form>
It still looks like the “grand total” loop is inside the “total” loop, so you’re calculating the grand total multiple times.