如何将php变量传递给ajax成功函数并分配给JavaScript变量

时间:2019-11-25 09:30:04

标签: javascript php ajax

我想在前端ajax成功回调函数中访问后端php变量。该怎么办? 我的代码php代码

    if($_POST["action"] == 'check_ot_count')
      { 

        $totaltime = 0;

        $ot_hours = $_POST["test"];
        $month = $_POST["month"];

         foreach ($ot_hours as $time_val) {
            $totaltime +=explode_time($time_val); // this fucntion will convert all hh:mm to seconds
         }
          $tablecount = second_to_hhmm($totaltime);
          $approval = GetApprovedOt($connect,$_SESSION["dept_Id"],$month);

          if($approval == ':00'){

            echo 'Before the time allocate you need get approval department group OT request';
          }else{
          if($approval < $tablecount){

          list ($hour1, $min1) = explode(':', $tablecount);
          list ($hour2, $min2) = explode(':', $approval);

         $sumHour = sprintf('%02d', $hour1 - $hour2);
         $sumMin = sprintf('%02d', $min1 - $min2);

         $temp = $sumHour.':'.$sumMin;
         //$sumSec = sprintf('%02d', $sec1 - $sec2);
            echo 'You Need to get Sub OT Approval '.$temp.' Hours to Time allocate in the department';
          }
          elseif($approval > $tablecount){
          list ($hour1, $min1) = explode(':', $approval);
          list ($hour2, $min2) = explode(':', $tablecount);

         $sumHour = sprintf('%02d', $hour1 - $hour2);
         $sumMin = sprintf('%02d', $min1 - $min2);
         $temp01 = $sumHour.':'.$sumMin;

            echo 'You can allocate time period succefully.Anyway '.$temp01.' Hours are still avilable to allocate' ;
          }elseif($approval == $tablecount){

            echo 'You are fully allocate the approval OT hours count';
          }
        }
      }
}

Java脚本代码

$.ajax({
      url: 'ot_divide_action.php',
      type: 'POST',
      data: { action:'check_ot_count', test:test,month:month},
      success:function(data)
      {
      /**/
   if(data == 'Before the time allocate you need get approval department group OT request')
   {
    setTimeout(function () { 
   swal({
     title: "OverTime Status!",
     text: "Before the time allocate you need get approval department group OT request",
     type: "warning",
     confirmButtonText: "OK"
    },
 function(isConfirm){
    if (isConfirm) {
       //window.location.href = "index.php";
      }
   }); }, 1);
    }/*no month procces if end*/
  else if (data == 'You are fully allocate the approval OT hours count'){
  setTimeout(function () { 
   swal({
     title: "OverTime Status!",
     text: "You are fully allocate the approval OT hours count",
     type: "success",
     confirmButtonText: "OK"
    },
 function(isConfirm){
    if (isConfirm) {
       //window.location.href = "index.php";
      }
   }); }, 1);
    }
 else if (data == "You Need to get Sub OT Approval" + <?php echo $temp; ?> + "Hours to Time allocate in the department"){
  var temp = <?php echo $temp; ?>;
  setTimeout(function () { 
   swal({
     title: "OverTime Status!",
     text: "You Need to get Sub OT Approval "+ temp + "Hours to Time allocate in the department",
     type: "info",
     confirmButtonText: "OK"
    },
 function(isConfirm){
    if (isConfirm) {
       //window.location.href = "index.php";
      }
   }); }, 1);
    }
  else if (data == 'You can allocate time period succefully.Anyway '.$temp01.' Hours are still avilable to allocate'){
    var temp01 = 0;
  setTimeout(function () { 
   swal({
     title: "OverTime Status!",
     text: "You can allocate time period succefully.Anyway "+ temp01 +" Hours are still avilable to allocate",
     type: "info",
     confirmButtonText: "OK"
    },
 function(isConfirm){
    if (isConfirm) {
       //window.location.href = "index.php";
      }
   }); }, 1);
    }
   }
  });

我想在不同条件下获取4种类型的警报框,但是前两个警报框正在工作,但是最后2个警报由于php变量未正确进入Ajax成功回调函数而无法工作,我该如何解决以上问题?

1 个答案:

答案 0 :(得分:1)

您正在POST请求中声明一个变量$temp。因此,现在基本上要插入这个变量了-这需要在初始GET请求中完成,该请求是在构建资源(例如包含您的JS的index.html)的情况下进行的。

您在这里有两个选择。

  1. 在投放内容时注入令牌。由于您不应该使用内联JS,因此可以包括一个包含令牌/日期的隐藏字段,以在以后的POST请求中签入。
  2. 通过单独的AJAX调用提前向POST请求获取令牌/日期。

常规:不应使用纯文本响应-尤其是在使用JS处理它们时。更好地将响应编码为JSON,从而可以给出结构化响应。请参阅json_encode(https://www.geeksforgeeks.org/php-json_encode-function/)。