By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

Grand total as an input field to forward to php

P: 3
I found this awesome piece of code and it works like a charm for what I want, but if I try to make the gTotal calculate as an input field as well it will not work at all...doesn't matter if I grab it by it's id either or anything...everything I try breaks the gTotal calculation...What I need is for the gTotal to be calculated into an input field so that my php page can grab it's name and post the gTotal.
here is the code:

Expand|Select|Wrap|Line Numbers
  1. function update(el)
  2. {
  3. var tr = el.parentNode;
  4. while (tr.nodeName.toLowerCase() != 'tr'){
  5. tr = tr.parentNode;
  6. }
  7.  
  8. var qty = tr.cells[1].getElementsByTagName('input')[0].value;
  9. var cost = tr.cells[2].getElementsByTagName('input')[0].value;
  10. var total = Math.round(qty*cost*100);
  11.  
  12. tr.cells[3].getElementsByTagName('input')[0].value = cToD(total);
  13.  
  14. var rows = tr.parentNode.rows;
  15. var i = rows.length;
  16. var gTotal = 0;
  17. while (i--){
  18. gTotal += rows[i].cells[3].getElementsByTagName('input')[0].value * 100;
  19. }
  20. document.getElementById('grandTotal').innerHTML = cToD(gTotal);
  21. }
Aug 14 '14 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you have to modify the cToD() function.
Aug 14 '14 #2

P: 3
Thank you for the response I tried that... I changed the function to a few old ones I had which:
Expand|Select|Wrap|Line Numbers
  1. function formatCurr(n) {
  2.     return n.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
  3. }
  4. function dm(amount) 
  5. {
  6. string = "" + amount;
  7. dec = string.length - string.indexOf('.');
  8. if (string.indexOf('.') == -1)
  9. {return string + '.00';}
  10. if (dec == 1)
  11. {return string + '00';}
  12. if (dec == 2)
  13. {return string + '0';}
  14. if (dec > 3)
  15. {return string.substring(0,string.length-dec+3);}
  16. return string;
  17. };
neither of them help me...
I changed the gTotal to:

Expand|Select|Wrap|Line Numbers
  1. tr.cells[4].getElementsByTagName('input')[0].value = dm(gTotal);
  2. document.getElementById('gTotal').value  = dm(gTotal);
neither of them work either... Sorry I guess I just don't understand this type of coding...more of a UIX than a functional coder...although I have my form 99% working. :)
Aug 14 '14 #3

P: 3
I figured it out finally...so in case anyone else has this issue here is the fix and it is so simple it's embarassing. lol. :)

Expand|Select|Wrap|Line Numbers
  1. document.purchreq.gTotal.value = formatCurr(gTotal);
Aug 19 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.