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!

Getting totals value from 10 Functions 1

Status
Not open for further replies.

Corneliu

Technical User
Sep 16, 2002
141
US
I am building this invoice and I got help from someone in ASP forum, but since he wrote the code in Javascript, I will ask here, maybe someone can help me please:

I want to add a totals at the bottom of the invoice, but dont know how. I dont work with Javascript much, so I have no clue how to get this to work. Code was given to me by someone from Tek-Tips:

<form name=&quot;frmINVOICE&quot;>
<input name=&quot;frmQTY&quot;>
<input name=&quot;frmDESC&quot;>
<input name=&quot;frmPRICE&quot; onblur=&quot;getTotal(frmINVOICE.frmQTY.value,frmINVOICE.frmPRICE.value)&quot;>
<input name=&quot;frmTOTAL&quot;>
</form>

<script language=&quot;Javascript&quot;>
function getTotal(val1,val2){
document.frmINVOICE.frmTOTAL.value = val1 * val2;
}
</script>


Basically I have 10 entries and each one has a different name. What I did at the end of the script is this:

function getTotal(val1,val2){
document.frmINVOICE.frmTOTAL.value = val1 * val2;}
function getTotal1(val1,val2){
document.frmINVOICE.frmTOTAL1.value = val1 * val2;
}
function getTotal2(val1,val2){
document.frmINVOICE.frmTOTAL2.value = val1 * val2;
}

so on until I get to 10. What I want is to add an Input box at the bottom of the page where the total would be. Eveything works great but I do not know how to add the last code to make the totals appear. Tried some things, but again I dont work much with Javascript.

THANK YOU FOR YOUR HELP...
 
Code:
function grandTotal() {
   var gt = parseInt(frmINVOICE.frmTOTAL.value);
   for (i = 1; i < 10; i++) {
      gt += parseInt(frmINVOICE[&quot;frmTOTAL&quot; + i].value);
   }
   //let's assume there's a field called frmGRANDTOTAL
   frmINVOICE.frmGRANDTOTAL.value = gt;
}
Since you said that you had 10 functions and the first one isn't numbered, I'm gonna assume the others go from 1 - 9. If they go from 1 - 10 just bump up the number in the for loop.

You can call this function at the end of each of your individual functions so that the grand total is constantly updated.

-or-

If all the functions to calculate totals are called at the same time, just call this function once afterwards.

-kaht

banghead.gif
 
kaht, I want to thank you for your time, but I am trying to figure out where the code goes and how to rename the fields. Sorry but I am not that well with Javascript. Should probably learn more. Anyways, here is what I have. I tried different ways to get the names properly, but no luck. Maybe you or someone can help me further, I would appreciate it very much:

The Form:
<Input name=&quot;QTY1&quot;>
<input name=&quot;FrmDESC1&quot; type=&quot;text&quot; value=&quot;&quot;>
<Input name=&quot;PRICE1&quot; onBlur=&quot;getTotal1
(INVOICE.QTY1.value,INVOICE.PRICE1.value)&quot;>
<input type=&quot;text&quot; name=&quot;TOTAL1&quot; disabled onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>

<Input name=&quot;QTY2&quot;>
<input name=&quot;FrmDESC2&quot; type=&quot;text&quot; value=&quot;&quot;>
<Input name=&quot;PRICE2&quot; onBlur=&quot;getTotal2
(INVOICE.QTY2.value,INVOICE.PRICE2.value)&quot;>
<input type=&quot;text&quot; name=&quot;TOTAL2&quot; disabled onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>

So on until I get to 10.
Then, I have the grantotal field box:
<Input name=&quot;INVOICETOTAL&quot; type=&quot;text&quot; disabled id=&quot;INVOICETOTAL&quot; onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>


Then the code after the form:
<script language=&quot;JavaScript&quot;>
function getTotal1(val1,val2){document.INVOICE.TOTAL1.value =val1 * val2;}
function getTotal2(val1,val2){document.INVOICE.TOTAL2.value =val1 * val2;}
function getTotal3(val1,val2){document.INVOICE.TOTAL3.value =val1 * val2;}
function getTotal4(val1,val2){document.INVOICE.TOTAL4.value =val1 * val2;}
function getTotal5(val1,val2){document.INVOICE.TOTAL5.value =val1 * val2;}
function getTotal6(val1,val2){document.INVOICE.TOTAL6.value =val1 * val2;}
function getTotal7(val1,val2){document.INVOICE.TOTAL7.value =val1 * val2;}
function getTotal8(val1,val2){document.INVOICE.TOTAL8.value =val1 * val2;}
function getTotal9(val1,val2){document.INVOICE.TOTAL9.value =val1 * val2;}

</script>



I have tried this after the first function.getTotal1 line:

function INVOICETOTAL() {
var gt = parseInt(INVOICE.TOTAL1.value);
for (i = 1; i < 10; i++) {
gt += parseInt(INVOICE[&quot;TOTAL1&quot; + i].value);
}
//let's assume there's a field called frmGRANDTOTAL
INVOICE.getTotal1.value = gt;
}

But I do not get any numebers in the last INVOICETOTAL field.

ANY HELP PLEASE????
THANK YOU VERY MUCH FOR THE HELP...
 
There are a few things wrong with your code that I can immediately see, but first off the most important thing to ask is why you're declaring 10 different functions that all do the same thing. I see that they are changing the value of 10 different fields on your page, but you can pass the field to the function so that you don't have to call a different function each time.

Next, inside the for loop on the INVOICETOTAL function you are trying to define your fields like this: &quot;TOTAL1&quot; + i This will yield results like this: TOTAL11, TOTAL12, TOTAL13, etc....

And finally, you said that you're not getting anything in your INVOICETOTAL field, but on the INVOICETOTAL function you are trying to set the field like this: INVOICE.getTotal1.value = gt That line doesn't reference the INVOICETOTAL field at all.

So..... to remedy this situation this is how I would code everything into one function:
Code:
function getTotal(val1, val2, obj) {
   obj.value = val1 * val2;
   var gt = 0;
   for (i = 1; i < 11; i++) {
      gt += parseInt(INVOICE[&quot;TOTAL&quot; + i].value);
   }
   INVOICE.INVOICETOTAL.value = gt;
}
and when calling this function be sure to pass a reference to which total field you are trying to fill:
Code:
<Input name=&quot;PRICE1&quot; onBlur=&quot;getTotal
    (INVOICE.QTY1.value,INVOICE.PRICE1.value,[b]INVOICE.TOTAL1)[/b]&quot;>

-kaht

banghead.gif
 
I get an error that INVOICE[...].value is not an object.

This is what I have.

The Form:
<Input name=&quot;PRICE1&quot; class=&quot;inputBoxsmall&quot; onBlur=&quot;getTotal
(INVOICE.QTY1.value,INVOICE.PRICE1.value,INVOICE.TOTAL1)&quot;>
<input type=&quot;text&quot; name=&quot;TOTAL1&quot; class=&quot;TotalItemPrice&quot; disabled align=&quot;right&quot; maxlength=&quot;10&quot; onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>

<Input name=&quot;PRICE2&quot; class=&quot;inputBoxsmall&quot; onBlur=&quot;getTotal
(INVOICE.QTY2.value,INVOICE.PRICE2.value,INVOICE.TOTAL2)&quot;>
<Input type=&quot;text&quot; name=&quot;TOTAL2&quot; class=&quot;TotalItemPrice&quot; disabled align=&quot;right&quot; maxlength=&quot;10&quot; onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>

So on until 10.
Then the total field:

<Input name=&quot;INVOICETOTAL&quot; type=&quot;text&quot; disabled class=&quot;inputTotals&quot; id=&quot;INVOICETOTAL&quot;
onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot; maxlength=&quot;10&quot; align=&quot;right&quot; onBlur=&quot;getTotal(INVOICE.INVOICETOTAL.value)&quot;>

The script:

<script language=&quot;JavaScript&quot;>

function getTotal(val1, val2, obj) {
obj.value = val1 * val2;
var gt = 0;
for (i = 1; i < 11; i++) {
gt += parseInt(INVOICE[&quot;TOTAL&quot; + i].value);
}
INVOICE.INVOICETOTAL.value = gt;
}
</script>

Did I do something wrong? THANK YOU VERY MUCH. This really helped me quite a lot.
THANK YOU...
 
Sorry, we're having speed issues this morning with my project at work. I'll try to help you out probably some time after lunch when I get some more spare time.

-kaht

banghead.gif
 
You can use this method, and I'm sure it'll work. However, you'll need to add a line to each of your TOTAL fields. They should look like this:
Code:
<input type=&quot;text&quot; name=&quot;TOTAL1&quot; [b]id=&quot;TOTAL1&quot;[/b] disabled onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot;>
and here's the fix:
Code:
function getTotal(val1, val2, obj) {
   obj.value = val1 * val2;
   var gt = 0;
   for (i = 1; i < 11; i++) {
      var blah = document.getElementById(&quot;TOTAL&quot; + i);
      gt += parseInt(blah.value, 10);
   }
   INVOICE.INVOICETOTAL.value = gt;
}

-kaht

banghead.gif
 
THANK YOU THANK YOU, worked great, but the lat piece I need PLEASE.

How do I make the totals field show up the total from all the TOTAL1, TOTAL2 FIELDS?

The coding for the total field is:

<Input name=&quot;INVOICETOTAL&quot; type=&quot;text&quot; disabled id=&quot;INVOICETOTAL&quot; onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot; onBlur=&quot;getTotal(INVOICE.INVOICETOTAL.value)&quot;>

Do I have to put an ID for the INVOICETOTAL input too? Or did I mess up and not put the proper code to show the totals?

THANK YOU AGAIN. I APPRECIATE IT VERY MUCH.
 
Kaht, I get a NaN in the totals field.
Does that help?

I had to add another line of entries because of the 10, I forgot it started at 1 and than I get NaN in the totals field.

Did I mess up somewhere?
Same code for the totals field as before:

<Input name=&quot;INVOICETOTAL&quot; type=&quot;text&quot; disabled id=&quot;INVOICETOTAL&quot; onKeyPress=&quot;return(currencyFormat(this,',','.',event))&quot; onBlur=&quot;getTotal(INVOICE.INVOICETOTAL.value)&quot;>


THANK YOU
 
you're probably getting NaN (not a number) because it's trying to add values from fields that don't have values yet, add this line to make the correction:
Code:
function getTotal(val1, val2, obj) {
   obj.value = val1 * val2;
   var gt = 0;
   for (i = 1; i < 11; i++) {
      var blah = document.getElementById(&quot;TOTAL&quot; + i);
[b]      //this checks for the existance of a value
      if (blah.value) {[/b]
         gt += parseInt(blah.value, 10);
[b]      }[/b]
   }
   INVOICE.INVOICETOTAL.value = gt;
}

-kaht

banghead.gif
 
THANK YOU THANK YOU THANK YOU.

WORKS LIKE A CHARM.

I am gonna try to figure out how to do the tax field now and I should be set.

I want to figure it out myself. Gotta learn it sometimes.

THANK YOU THANK YOU THANK YOU.

That really helped me a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top