0

I am trying to figure out how to add up a series of text boxes which contain numbers that a user will input and have the total of those text boxes show up in a quantity text box on the next page.

Not sure if i should use javascript or php, but i don't know how to do it either, so any help would be appreciated.

thanks, nigel

3
Contributors
5
Replies
8
Views
7 Years
Discussion Span
Last Post by scotttheath
0

First, you have to give each box an ID that you can easily use in a do-loop [for example, score1, score2, score3...] You also need to have a page element with the ID 'Total'. Here, FWIW, is what I have in my code (I happen to have 13 boxes):

function totalscore(){//set array containing scores
		chemtests = new Array();
		for(var i=0; i<13; i++){ 
			rownumber = i+1;	
			myscore=parseInt(document.getElementById('score' + (rownumber)).innerHTML)
			chemtests[i]=myscore
		}//end for loop
		//add up scores and stick them into the total box
		var sum = 0;
		for(var i=0; i<13; i++){
			sum=sum + parseInt(chemtests[i]);
		}
		document.getElementById('Total').innerHTML=sum
	}

Test this and see if it works for you. Also, I took out a few lines with irrelevant code, so make sure the brackets all match.

0

my apologies Scott, I thought I would get an email if someone answered my thread. I will try this out. So sorry I didn't get back sooner.

0
sum=sum + parseInt(chemtests[i]);

In that portion, use

sum=sum + parseInt(chemtests[i], 10);

instead because the parseInt has a bug when the number has leading 0 in front. Forcing it with base 10 would solve the problem.

0
sum=sum + parseInt(chemtests[i]);

In that portion, use

sum=sum + parseInt(chemtests[i], 10);

instead because the parseInt has a bug when the number has leading 0 in front. Forcing it with base 10 would solve the problem.

Thanks! So far, that bug hasn't bitten me yet, but I'll be on the lookout for it. The leading zero shouldn't be an issue, though, since the boxes are populated by another script, not by user input. For the original poster, though, it's a possibility to be reckoned with!

BTW, is this a universal problem, or just one with a specific Javascript implementation?

0

my apologies Scott, I thought I would get an email if someone answered my thread. I will try this out. So sorry I didn't get back sooner.

That's odd. I got an e-mail notifying me about your reply. Go figure (spam filters and company firewalls often seem to interfere with email).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.