/    Sign up×
Community /Pin to ProfileBookmark

Running sum of textboxes w for loop

Hi,
Have an example with an orderform for two products. The form has columns of textboxes. Split in a lot of sections (showing the first two as an example) These textboxes are then sent to a PHP script. But I want to validate that the form is correct first. The textboxes are named as

<form action=”process.php” method=”post”>
<tr>
<td> input name=”Amount1[]” id=”Amount1[]” />
<td> input name=”Amount2[]” id=”Amount2[]” />
</tr>

<tr>
<td> input name=”Amount1[]” id=”Amount1[]” />
<td> input name=”Amount2[]” id=”Amount2[]” />
</tr>

In the last section I have two textboxes “sum_amount1” and “sum_amount2”. Basically I want to add up all amount1[] boxes into sum_amount1. Not using textbox1 + textbox2 etc. but instead using a for loop.

Some expert coders around to help me?

Thanks in advance.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 27.2011 — First: Unlike the [B]name[/B], [B]id[/B] must be unique on document, so that you can not have 2 or more elements with the same id

Now, your problem:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function add(){
var names={
'Amount1[]':'sum_amount1',
'Amount2[]':'sum_amount2'
}
var s, i, t;
for(n in names){
s=0;i=0;
while(t=document.getElementsByName(n)[i++]){
s+=Number(t.value);
}
document.getElementsByName(names[n])[0].value=s;
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=""&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type="text" name="Amount1[]"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="Amount2[]"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type="text" name="Amount1[]"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="Amount2[]"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type="text" name="sum_amount1" readonly="readonly"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="sum_amount2" readonly="readonly"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type="button" value="Sum up" onclick="add()"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@rnasmanauthorJan 27.2011 — Awesome! I can definitely use this in my validation. Thanks a lot!
×

Success!

Help @rnasman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...