0

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

3
  • 2
    First you iterate, then you send the ajax request. You don't put the ajax request inside the loop. Commented Nov 22, 2019 at 11:50
  • You need to make the variable global, declare it outside the each scope. Commented Nov 22, 2019 at 12:00
  • what variable should define in globaly Commented Nov 22, 2019 at 12:01

1 Answer 1

2

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"});
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.