Forum Moderators: open

Message Too Old, No Replies

Addition of a range of variables in an array

Addition of a range of variables in an array

         

isthmus

3:03 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



Hi Everyone,

I have been struggling with this for a few days now and am almost ready to give up... Any advice is most appreciated!

What I am trying to do is is to calculate a subtotal based on a range of items in an array. The range is to be specified by the user by way of text boxes. Here is as far as I have got:


<form name="subtotal">

<script>

function grabsubtotal()
{

var lowest = document.subtotaller.lowest.text
var highest = document.subtotaller.highest.text

alert ('the subtotal is $total');

}

</script>

Enter Lowest Value:<input type="text" name="lowest" size="5"><BR>
Enter Highest Value:<input type="text" name="highest" size="5"><BR>
<input type="button" name="activate" value="get total" onClick="javascript:grabsubtottaler();">

<input type="hidden" name="ENV[370]" value="10.00">
<input type="hidden" name="ENV[10]" value="10.00">
<input type="hidden" name="ENV[10]" value="10.00">
<input type="hidden" name="ENV[20]" value="10.00">
<input type="hidden" name="ENV[30]" value="10.00">
<input type="hidden" name="ENV[40]" value="10.00">

</form>

Not very far at all, I know. You will notice that the keys have an assigned value and that some keys may appear multiple times.

Perhaps I am going about this the wrong way... let me know what you think.

Thank you!

Mitchell

Bernard Marx

7:55 pm on Apr 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi. This is a little confusing

..calculate a subtotal based on a range of items in an array

Where's the array?

document.subtotaller.lowest.text

I think you mean:

document.subtotal.lowest.value

alert ('the subtotal is $total');

What language is that? PHP?
When you have your total, it'll be more like:

alert("the subtotal is " + total)

onClick="javascript:grabsubtottaler()

1. Your function is called grabsubtotal. Sloppy!
2. You don't need the 'javascript:' protocol part.

What do all the hidden fields do?

Power to your elbow.

isthmus

8:52 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



Hi Bernard,

Thank you for the reply. Upon reading your post and then re-reading my post I see why you would be confused. I have been struggling with this problem for a few days and my frustration lead to an incomplete post. Let me flesh out the problem completley:

I have a page, output from php, that gives me a set of rows that look like this:

first name ¦ last name ¦ dollar amount ¦ id#

I have created a hidden form element to go along with each row, that looks like this:

<input type="hidden" name="id#[370]" value="dollaramount">

So the array is that collection of hidden variables which have the same name and key declared.

Now, what I want to do is to set up a way for someone to quickly view subtotals based on a range of ids. That is where the form elements for lowest and highest come in.

The user enters the lower number a 5 and the highest as 100 and then the script would count through the amounts for the entries with id numbers between those values and then output a number.

I tried a php solution first, which I am stronger at, but it doesn't work well for this to have to reload. JavaScript therefore seems the best solution.

Does that make more sense? Any advice is most appreciated...

Thank you,

Mitchell