Code Kris
Code Kris

Reputation: 447

how to push the each loop variable value in to the array

My problem is how do push each loop variable value in to the single array?(my code is not working)

$(document).on('click', '.overtime_check', function() {
   //temp = 0;

  $('#ot_send_table tbody tr').each(function(row, tr) {
    var test = [];
    var emp_no = $(tr).find('td:eq(0)').text();
    var ot_hours = $(tr).find('input').val();
    test.push(ot_hours);

    });
        $.ajax({
              url: 'ot_divide_action.php',
              type: 'POST',
              data: { action:'check_ot_count', test:test},
              //dataType:"json",
              success:function(data)
              {
              //swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});
              $('#display-output').html(data);
             }
          });
   //swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});

});

i want to sent below format of array through ajax request

$time_arr =  [
'00:02:55',
'00:07:56',
'01:03:32',
'15:13:34',
'02:13:44',
'03:08:53',
'13:13:54'
 ];

how do i do it ?

update Code

Upvotes: 0

Views: 176

Answers (1)

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

Your problem is that you define the var test = []; array inside the .each() callback. You need to define it before the iteration. Try this one:

$(document).on('click', '.overtime_check', function() {
    //temp = 0;

    var test = []; // Here, the array declaration should be outside the each()

    $('#ot_send_table tbody tr').each(function(row, tr) {
        var emp_no = $(tr).find('td:eq(0)').text();
        var ot_hours = $(tr).find('input').val();
        test.push(ot_hours);
    });

    $.ajax({
        url: 'ot_divide_action.php',
        type: 'POST',
        data: { action:'check_ot_count', test:test},
        //dataType:"json",
        success: function (data) {
            //swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});
            $('#display-output').html(data);
        }
    });
    //swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});
});

Upvotes: 2

Related Questions