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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amek000
    New Member
    • Sep 2010
    • 5

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

    Code:
    <script language="javascript">
    function calcvaluesArr(){
    var txts = document.getElementsByName('textfield23[]');
    		var aTotal=0;                      
    		 for (var i = 0; i < txts.length; i++) 
              {         
                if (txts[i].value!="") 
                   { 
    				aTotal=eval(aTotal)+eval(txts[i].value);
                  } 
            } 
    document.form.textfield19.value=aTotal;
    }
    </script>
    Last edited by Niheel; Sep 15 '10, 06:04 PM. Reason: code tags
  • Canabeez
    New Member
    • Jul 2009
    • 126

    #2
    Here, look into this, believe this might help you:[code="html"]
    <script type="text/javascript">
    function calculate(){
    var textFields = document.getEle mentsByName('tx t');
    var total = 0;

    for(var i in textFields){
    if(textFields[i].value){
    total += parseFloat(text Fields[i].value);
    }
    }

    document.getEle mentById('total ').value = total;
    }

    window.onload = function(){ calculate(); };
    </script>
    <input type="text" name="txt" value="1" onkeyup="calcul ate()" />
    <input type="text" name="txt" value="2" onkeyup="calcul ate()" />
    <input type="text" name="txt" value="3" onkeyup="calcul ate()" />
    <input type="text" name="txt" value="4" onkeyup="calcul ate()" />
    <input type="text" name="txt" value="5" onkeyup="calcul ate()" />

    <input type="text" name="total" id="total" value="0" readonly />[/code]

    Good luck.

    Comment

    • amek000
      New Member
      • Sep 2010
      • 5

      #3
      Thanks Canabeez, but your solution worked partially

      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

      Comment

      • Canabeez
        New Member
        • Jul 2009
        • 126

        #4
        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.

        Comment

        • amek000
          New Member
          • Sep 2010
          • 5

          #5
          It works good in Mozilla. It does not give any error in IE and Chrome but doesnt work

          Code:
          <?php
          
          foreach($textfield23 as $a => $b){
          $qins = "INSERT INTO myTable VALUES(NULL, $reqid, $selecta[$a], '$textfield23[$a]', '', '', NOW())";
          mysql_query($qins) or die(mysql_error());
          ";
          
          }
          
          ?>

          Comment

          • amek000
            New Member
            • Sep 2010
            • 5

            #6
            It does not give any JS errors

            No JS errors are are showing

            Comment

            • amek000
              New Member
              • Sep 2010
              • 5

              #7
              This is the JavaScript

              Code:
              function calculate(){
                      var textFields = document.getElementsByName('textfield23');
                      var total = 0;
              
                      for(var i in textFields){
                          if(textFields[i].value){
                              total += parseFloat(textFields[i].value);
                          }
                      }
              
                      document.getElementById('textfield19').value = total;
                  }
              	
                  window.onload = function(){ calculate(); };

              Comment

              Working...