ajax data.success block of code not be execute(variable value)

Viewed 51

i have big problem in regrading ajax request .if the ajax request success i want to set variable value as 1 if not successes it remaining the as 0 .but my program is not working properly .what is problem and how to solved is problem?

$(document).on('click', '.overtime_send', function() {
   temp = 0;
  $('#employee_table tbody tr').each(function(row, tr) {

    var emp_no = $(tr).find('td:eq(0)').text();
    var ot_hours = $(tr).find('input').val();

    //ot_array.push([emp_no,ot_hours]);

    $.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_ot',emp_no : emp_no, ot_hours:ot_hours},
      dataType:"json",
      success:function(data)
      {
        if(data.success)
        {
         swal("Good job!", "OverTime Request Send Successfully!", "success");
         temp = 1;
         dataTable.ajax.reload();  
        }
      }
    });  
  });
  alert(temp);//data.success alert display but value is still 0  
  if (temp == 1) {
$.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_comment'},
      dataType:"json",
      success:function(data)
      {
        if(data.success)
        {  
         dataTable.ajax.reload();
        }
      }
    });
}
     swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});

}); 
3 Answers
(document).on('click', '.overtime_send', function() {
   temp = 0;
  $('#employee_table tbody tr').each(function(row, tr) {

    var emp_no = $(tr).find('td:eq(0)').text();
    var ot_hours = $(tr).find('input').val();

    //ot_array.push([emp_no,ot_hours]);

    $.ajax({
      url: 'otrequset_action.php',
      type: 'POST',
      data: { action:'add_ot',emp_no : emp_no, ot_hours:ot_hours},
      dataType:"json",
      success:function(data)
      {
         swal("Good job!", "OverTime Request Send Successfully!", "success");
         temp++;
         dataTable.ajax.reload();
           if (temp == 1) {
           $.ajax({
              url: 'otrequset_action.php',
              type: 'POST',
              data: { action:'add_comment'},
              dataType:"json",
              success:function(data)
              {
                if(data.success)
                {  
                 dataTable.ajax.reload();
              }
             }
          });
        }  
    }
   });  
});

   swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});

});

What is the purpose of data.success if condition in success function of first ajax request. Are you setting it up in php code and returning back?

success:function(data)
      {
        if(data.success)
        {
         swal("Good job!", "OverTime Request Send Successfully!", "success");
         temp = 1;
         dataTable.ajax.reload();  
        }
      }

i don't think you need that if condition. Remove that condition and then try.

Moreover create an error function for handling not success cases.

$(document).on('click', '.overtime_send', function() {
    temp = 0;
    $('#employee_table tbody tr').each(function(row, tr) {
        var emp_no = $(tr).find('td:eq(0)').text();
        var ot_hours = $(tr).find('input').val();

        $.ajax({
            url: 'otrequset_action.php',
            type: 'POST',
            data: { action:'add_ot',emp_no : emp_no, ot_hours:ot_hours},
            dataType:"json",
            success:function(data) {
                if(data.success) {
                    swal("Good job!", "OverTime Request Send Successfully!", "success");

                    if (temp != 1) {
                        $.ajax({
                            url: 'otrequset_action.php',
                            type: 'POST',
                            data: { action:'add_comment'},
                            dataType:"json",
                            success:function(data) {
                                if(data.success) {
                                    dataTable.ajax.reload();
                                }
                            }
                        });
                    }

                    temp = 1;
                    dataTable.ajax.reload();
                }
            }
        });
  });

  swal({title: "No data entering?",text: "Not set the overtime hours !",type: "warning"});
});

Ajax will execute success function only when the request is successful (which means success will be executed after a delay). But the remaining code will run even though ajax is not complete (Asynchronous execution). By the time you print it or use it, it will still be 0;

Related