Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing summation script for a button and forms

Status
Not open for further replies.

gman13

Programmer
Oct 9, 2001
10
US
I have a page with many forms that take in integer values. Then at the bottom I have a button that totals the integers in all the forms and puts it in a form at the bottom. My button is not working. How do I creat a script that sums all the integer values from the forms? Any help would be greatly appreciated.
Garrick
 
Hi Garrick,

Firstly, let us know what you've got now... maybe we can help you fix what you've already got, rather than re-invent something from scratch.

regards,

Graham
 
if all of the elements in your form have a value then you can loop thruough the forms collection and for each form found loop thru the elements collection at the same time adding the found element values.
 
Well i tried incorporating a small javascript fcn, but it doesn't seem to want to work right. I don't know if I'm callin my vars the right name. I am using the button name since it accepts an integer val(i am not the best js programer out there, in case you can't tell...I'll stick with my assembly and C). then I take that and use document.write(total(vals here)) and set that to the form field that the total goes into. One problem I see is that I'm not sure how the button figures in. I put the js function in the button code, but it doesnt help. It just errors out. well there you have it.
Garrick
 
try the following, i only tested it in ie but it should only require a little if any tweaking for netscape

<html>
<head>
</head>
<body margintop=0>
<script>
function Addit()
{
var frmlen = document.forms.length;
var x=0;
var i=0;
var j=0;
for (i=0; i<frmlen;i++)
{
var inputlen = document.forms.elements.length;
alert(&quot;Form &quot;+Number(i+1)+&quot; has &quot;+inputlen+&quot; elements&quot;)
for (j=0; j<inputlen;j++)
{
if (document.forms.elements[j].type==&quot;text&quot;)
{
alert(CheckNumber(document.forms.elements[j].value))
x = x + Number(CheckNumber(document.forms.elements[j].value))
}
}
}
alert(&quot;TOTAL=&quot;+x);
}
function CheckNumber(TheNumber) {
var valid = 1
var GoodChars = &quot;0123456789&quot;
var i = 0
if (TheNumber==&quot;&quot;) {
// Return false if number is empty
valid = 0
}
for (i =0; i <= TheNumber.length -1; i++) {
if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
valid = 0
} // End if statement
} // End for loop
if (valid ==0) { return 0 }
else { return TheNumber }

}

</script>
form1
<form>
<input type=text><br>
<input type=text><br>
<input type=text><br>
<input type=text><br>
</form>
form2
<form>
<input type=text><br>
<input type=text><br>
<input type=text><br>
<input type=text><br>
</form>
form3
<form>
<input type=text><br>
<input type=text><br>
<input type=text><br>
<input type=text><br>
</form>
<input type=button onclick=&quot;Addit()&quot; value=&quot;Total&quot;>
</body>
</html>
 
Thanks for the help. I have put it into implementation and I it works fine.

Garrick
 
Well SJravee has given you the detailed answer. But i don't know wether your problem is solved by this or not. If not post the code that u have so far and then We'll(all the members) Insha-Allah try to solve it.

Best of Luck Momin ho to bay taigh bhee lerta hay sipahee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top