我正在使用OT系统。我在升级OT小时请求程序时遇到一些问题。此程序将部门员工的OT小时发送到管理面板通知中,并将OT小时保存在数据库中。我的问题是当发送OT请求时每个人都创建了单独的OT通知。我要发送部门人员的单个通知集。

$(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);//if data.success block execute but alert still get value as 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();
        }
      }
    });
}


我都准备好了一些解决方案。我成功创建了temp变量,并且OT请求过程成功分配了temp = 1,并在每个循环之后尝试将通知数据插入数据库。但是,如果data.success成功,但是alert(temp);没有获得价值作为1。我该如何解决这个问题。

后端代码

if($_POST["action"] == 'add_ot')
      {
        $today = date("Y-m-d");
        $department = $_SESSION["dept_Id"];
        $state = 5 ;
        $emp_no = $_POST["emp_no"];
        $ot_hours = $_POST["ot_hours"];

        if($ot_hours != '00:00'){

        $data = array(
          ':today'                =>  $today,
          ':department'           =>  $department,
          ':emp_no'               =>  $emp_no,
          ':ot_hours'             =>  $ot_hours,
          ':state'                =>  $state

        );
        $query = "
        INSERT INTO otresquest
        (emp_id,date,ot_hour,dept_Id,overtime_status)
        VALUES (:emp_no, :today, :ot_hours,:department,:state)
        ";
        $statement = $connect->prepare($query);
        if($statement->execute($data))
        {
          $output = array(
            'success'   =>  'OT request has been Sent succesfully',
          );
        }
      }
        echo json_encode($output);
      }

  if($_POST["action"] == 'add_comment')
      {
        $today = date("Y-m-d");
        $department = $_SESSION["dept_Id"];
        $comment_status = 0 ;

        $data = array(
          ':today'                =>  $today,
          ':department'           =>  $department,
          ':state'                =>  $comment_status

        );
        $query = "
        INSERT INTO comments
        (dept_Id,date,comment_status)
        VALUES (:department,:today,:state)
        ";
        $statement = $connect->prepare($query);
        $statement->execute($data);
    }

最佳答案

**This might be solve your 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)
      {
         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"});

});

10-08 16:36