Saving inputs to db after auto sum

64 views Asked by At

Im trying to save some values from input fields to my db after ive used js to sum them all up. I want to save both the inputs and the total sum and theres when my code breaks down. All my inputs are empty after the autosum has run and its only the sum of the fields that get saved to db. I'm not sure how to fix this, im hoping someone out there have an solution. Here's my code.

If this is a stupid way of doing it please show me the right path!

<td>        
  <div class="form-group">
    {{ Form::text('report_mon_time', Input::old('report_mon_time'), array('name' => 'time', 'class' => 'form-control-s', 'onblur' => 'findTotalTime()')) }}
  </div>
</td>
<td>
  <div class="form-group">
    {{ Form::text('report_tue_time', Input::old('report_tue_time'), array('name' => 'time', 'class' => 'form-control-s', 'onblur' => 'findTotalTime()')) }}
  </div>
</td>
  <div class="form-group">
    {{ Form::text('report_tot_time', Input::old('report_tot_time'), array('id' => 'tot_time','class' => 'form-control-s')) }}
</div>

<script>
function findTotalTime(){
    var number = document.getElementsByName('time');
    var arr = number;
    var tot=0;

    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('tot_time').value = tot;
}
</script>
1

There are 1 answers

2
Billy On

Change

for(var i=0;i<arr.length;i++){ for(var i=0;i<arr.length;i++){
    if(parseInt(arr[i].value))
        tot += parseInt(arr[i].value);
}

To

 for(var i=0;i<number.length;i++){
    if(parseInt(number[i].value))
        tot += parseInt(number[i].value);
}