Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to display the total of the columns in the total column

on changing the values the total have to calculate automatically and display in the total column


Here is my code please Guide using javascript onblur

PHP
<?php
while($res=mysql_fetch_array($gettimesheetentries)){?>
<tr>
<td><input type="text" name="data1" id="data1" size="10" value="<?php echo $res['data1'];?>" ></td>
<td><input type="text" name="data2" id="data2" size="10" value="<?php echo $res['data2'];?>" ></td>
<td><input type="text" name="data3" id="data3" size="10" value="<?php echo $res['data3'];?>" ></td>
<td><input type="text" name="data4" id="data4" size="10" value="<?php echo $res['data4'];?>" ></td>
<td><input type="text" name="total" id="total" size="10" ></td>
</tr>
 <?php } ?>
Posted
Comments
ZurdoDev 11-Jun-15 8:55am    
There's no magic way to do it, you'll have to write code to get each value and then add it all up.

select sum(columname) As col from [tablename];
 
Share this answer
 
The total column cell should not be editable, so a plain text cell will do
<td class="total"></td>

then, use jQuery to calculate and update the row sum either onload or onblur:
XML
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").each(function() {

        var that = $(this);

        // sum and display total column
        sumUp(that);
        
        // bind onblur to each input text cell
        $(this).blur(function(){
            sumUp(that);
        });
    });
});

function sumUp(obj) {
   var sum = 0;
   var focusedRow = obj.closest('tr');

   focusedRow.find('input:text').each( function(){
        sum += parseFloat(this.value);
   });

   focusedRow.find('td.total').html(sum);
}
</script>
</head>

Reference: https://learn.jquery.com/events/event-basics/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900