L
lawrencec
Hi there,
I'm trying to add the values of a number of form fields and to get a
result at the end. It must loop and be able to dynamically update the
result of calculation. I have attached the code i have so far.
Thanks
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript1.2" type="text/JavaScript">
function calculate(currid)
{
var rownumber = "";
var Qnt;
var Qnt_Total;
rownumber = currid.substring(currid.indexOf("_", currid) + 1);
//DEBUGGING
//alert(rownumber);
//return false;
Qnt = document.getElementById("Quantity_" + rownumber).value;
for(var j = 0 ; j <= document.CaptureForm.elements.length ; j++)
{
cnt = j + 1;
//if(eval(document.CaptureForm.elements[j].id) == "[object]")
if(document.CaptureForm.elements[j].id ==
"Quantity_"+String(cnt))
{
if(document.CaptureForm.elements[j].value != "")
Qnt_Total += Number(document.CaptureForm.elements[j].value);
}
}
document.CaptureForm.Totals1.value = Qnt_Total;
}
</script>
</head>
<body>
<form action="" method="post" name="CaptureForm">
<SPAN id="Quantity_Label" style="LEFT: 518px; WIDTH: 62px; COLOR:
#000000;POSITION: absolute; TOP: 186px; HEIGHT: 13px; BACKGROUND-COLOR:
transparent; FONT-FAMILY:MS Sans Serif; FONT-SIZE:8pt;">Quantity</SPAN>
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 200; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity" id="Quantity_1" value="<?php echo
$row_Recordset1['Quantity']; ?>" onBlur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 226; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity2" id="Quantity_2" value="<?php echo
$row_Recordset1['Quantity2']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 252; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity3" id="Quantity_3" value="<?php echo
$row_Recordset1['Quantity3']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 279; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity4" id="Quantity_4" value="<?php echo
$row_Recordset1['Quantity4']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 305; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity5" id="Quantity_5" value="<?php echo
$row_Recordset1['Quantity5']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 331; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity6" id="Quantity_6" value="<?php echo
$row_Recordset1['Quantity6']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 358; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity7" id="Quantity_7" value="<?php echo
$row_Recordset1['Quantity7']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 384; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity8" id="Quantity_8" value="<?php echo
$row_Recordset1['Quantity8']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 410; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity9" id="Quantity_9" value="<?php echo
$row_Recordset1['Quantity9']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 436; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity10" id="Quantity_10" value="<?php echo
$row_Recordset1['Quantity10']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 463; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity11" id="Quantity_11" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 489; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity12" id="Quantity_12" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 493; text-align: right; WIDTH:
100px; COLOR: #000000;POSITION: absolute; TOP: 518; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Totals1" id="Totals1" value="<?php echo
$row_Recordset1['Totals1']; ?>">
</form>
</body>
</html>
I'm trying to add the values of a number of form fields and to get a
result at the end. It must loop and be able to dynamically update the
result of calculation. I have attached the code i have so far.
Thanks
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript1.2" type="text/JavaScript">
function calculate(currid)
{
var rownumber = "";
var Qnt;
var Qnt_Total;
rownumber = currid.substring(currid.indexOf("_", currid) + 1);
//DEBUGGING
//alert(rownumber);
//return false;
Qnt = document.getElementById("Quantity_" + rownumber).value;
for(var j = 0 ; j <= document.CaptureForm.elements.length ; j++)
{
cnt = j + 1;
//if(eval(document.CaptureForm.elements[j].id) == "[object]")
if(document.CaptureForm.elements[j].id ==
"Quantity_"+String(cnt))
{
if(document.CaptureForm.elements[j].value != "")
Qnt_Total += Number(document.CaptureForm.elements[j].value);
}
}
document.CaptureForm.Totals1.value = Qnt_Total;
}
</script>
</head>
<body>
<form action="" method="post" name="CaptureForm">
<SPAN id="Quantity_Label" style="LEFT: 518px; WIDTH: 62px; COLOR:
#000000;POSITION: absolute; TOP: 186px; HEIGHT: 13px; BACKGROUND-COLOR:
transparent; FONT-FAMILY:MS Sans Serif; FONT-SIZE:8pt;">Quantity</SPAN>
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 200; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity" id="Quantity_1" value="<?php echo
$row_Recordset1['Quantity']; ?>" onBlur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 226; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity2" id="Quantity_2" value="<?php echo
$row_Recordset1['Quantity2']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 252; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity3" id="Quantity_3" value="<?php echo
$row_Recordset1['Quantity3']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 279; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity4" id="Quantity_4" value="<?php echo
$row_Recordset1['Quantity4']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 305; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity5" id="Quantity_5" value="<?php echo
$row_Recordset1['Quantity5']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 331; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity6" id="Quantity_6" value="<?php echo
$row_Recordset1['Quantity6']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 358; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity7" id="Quantity_7" value="<?php echo
$row_Recordset1['Quantity7']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 384; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity8" id="Quantity_8" value="<?php echo
$row_Recordset1['Quantity8']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 410; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity9" id="Quantity_9" value="<?php echo
$row_Recordset1['Quantity9']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 436; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity10" id="Quantity_10" value="<?php echo
$row_Recordset1['Quantity10']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 463; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity11" id="Quantity_11" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 489; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity12" id="Quantity_12" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 493; text-align: right; WIDTH:
100px; COLOR: #000000;POSITION: absolute; TOP: 518; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Totals1" id="Totals1" value="<?php echo
$row_Recordset1['Totals1']; ?>">
</form>
</body>
</html>