...

View Full Version : Need help with loop to add numbers



salsalero
07-10-2006, 09:21 AM
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);

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>

vwphillips
07-10-2006, 01:32 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

var CalAry=[];

function InitForm(frm,fldnme,total){
var frm=document[frm];
var eles=frm.elements;
CalAry.push(frm[total]);
for (var zxc0=0;zxc0<eles.length;zxc0++){
if (eles[zxc0].name.match(fldnme)){
CalAry.push(eles[zxc0]);
zxcAddEvt(eles[zxc0],'Cal','keyup');
}
}
}

function Cal(){
if (isNaN(this.value)){
this.value=this.value.replace(/\D/g,'');
}
var val=0;
for (var zxc0=1;zxc0<CalAry.length;zxc0++){
if (CalAry[zxc0].value.length>0){
val+=CalAry[zxc0].value*1;
}
}
CalAry[0].value=val;
}


function zxcEventAdd(zxco,zxct,zxcf) {
if ( zxco.addEventListener ){ zxco.addEventListener(zxct, function(e){ zxco[zxcf](e);}, false); }
else if ( zxco.attachEvent ){ zxco.attachEvent('on'+zxct,function(e){ zxco[zxcf](e); }); }
else {
var zxcPrev=zxco["on" + zxct];
if (zxcPrev){ zxco['on'+zxct]=function(e){ zxcPrev(e); zxco[zxcf](e); }; }
else { zxco['on'+zxct]=zxco[zxcf]; }
}
}

var zxcEvt=0;

function zxcAddEvt(zxco,zxcfun,zxcevt){
if (zxco['zxc'+zxcfun+zxcevt]){ return; }
zxco['zxcaddEvt'+zxcEvt]=window[zxcfun];
zxco['zxc'+zxcfun+zxcevt]=true;
zxcEventAdd(zxco,zxcevt,'zxcaddEvt'+zxcEvt);
zxcEvt++;
}


/*]]>*/
</script>

</head>

<body onload="InitForm('contact','Calculate','Total');" >
<form name=contact >
<input name="Calculate1" />
<input name="Calculate2" />
<input name="Calculate3" />
<input name="Calculate4" />
<input name="Total" />
</form>

<div id="error">
</div>
</body>

</html>

salsalero
07-10-2006, 03:08 PM
Thanks Vic for your code.

I got another solution

<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 showTotal(form, totalEl)
{
var el, els = form.elements;
var Quantity = 0;


for (var i=0, num=els.length; i<num; ++i){
el = els[i];


if ('text' == el.type && /Quantity/.test(el.name)){
Quantity += +el.value;
}
form.elements[totalEl].value = Quantity;
}
}
</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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="showTotal(this.form, 'formTotal');">
<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="formTotal" id="Totals1" value="<?php echo $row_Recordset1['Totals1']; ?>">
</form>

</body>
</html>

vwphillips
07-10-2006, 03:29 PM
your 'solution' requires event calls to be added to each field and does not check for numbers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum