469,309 Members | 2,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

problem with code to calculate total values in textboxes that are added dynamically

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function calcvaluesArr(){
  3. var txts = document.getElementsByName('textfield23[]');
  4.         var aTotal=0;                      
  5.          for (var i = 0; i < txts.length; i++) 
  6.           {         
  7.             if (txts[i].value!="") 
  8.                { 
  9.                 aTotal=eval(aTotal)+eval(txts[i].value);
  10.               } 
  11.         } 
  12. document.form.textfield19.value=aTotal;
  13. }
  14. </script>
Sep 15 '10 #1
6 2257
Canabeez
126 100+
Here, look into this, believe this might help you:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function calculate(){
  3.         var textFields = document.getElementsByName('txt');
  4.         var total = 0;
  5.  
  6.         for(var i in textFields){
  7.             if(textFields[i].value){
  8.                 total += parseFloat(textFields[i].value);
  9.             }
  10.         }
  11.  
  12.         document.getElementById('total').value = total;
  13.     }
  14.  
  15.     window.onload = function(){ calculate(); };
  16. </script>
  17. <input type="text" name="txt" value="1" onkeyup="calculate()" />
  18. <input type="text" name="txt" value="2" onkeyup="calculate()" />
  19. <input type="text" name="txt" value="3" onkeyup="calculate()" />
  20. <input type="text" name="txt" value="4" onkeyup="calculate()" />
  21. <input type="text" name="txt" value="5" onkeyup="calculate()" />
  22.  
  23. <input type="text" name="total" id="total" value="0" readonly />
Good luck.
Sep 15 '10 #2
The issue now is:

1. Canabeez, your code does not run in IE8
2. I am not able to get the values inserted into mysql using php

Any ideas on these?

Thanks again
Sep 15 '10 #3
Canabeez
126 100+
Sorry, I'm a mac user, unable to check on IE8 :/ are you getting some JS errors?

Show some php/javascript/mysql code for further assistance.
Sep 15 '10 #4
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. foreach($textfield23 as $a => $b){
  4. $qins = "INSERT INTO myTable VALUES(NULL, $reqid, $selecta[$a], '$textfield23[$a]', '', '', NOW())";
  5. mysql_query($qins) or die(mysql_error());
  6. ";
  7.  
  8. }
  9.  
  10. ?>
  11.  
Sep 15 '10 #5
No JS errors are are showing
Sep 15 '10 #6
This is the JavaScript

Expand|Select|Wrap|Line Numbers
  1. function calculate(){
  2.         var textFields = document.getElementsByName('textfield23');
  3.         var total = 0;
  4.  
  5.         for(var i in textFields){
  6.             if(textFields[i].value){
  7.                 total += parseFloat(textFields[i].value);
  8.             }
  9.         }
  10.  
  11.         document.getElementById('textfield19').value = total;
  12.     }
  13.  
  14.     window.onload = function(){ calculate(); };
  15.  
Sep 15 '10 #7

Post your reply

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

Similar topics

3 posts views Thread by Forconi Boris via .NET 247 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.