简体   繁体   中英

how to pass php variable in to ajax success function and assign in to JavaScript variable

I want to access back end php variable in front end ajax success call back function.how do do it? my code php code

    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 script code

$.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);
    }
   }
  });

I want to get 4 type of alert box in different condition, first two alert box is working but last 2 alert does'nt work because of the php variable correctly not coming to the ajax success call back function, How I can solve the above issue?

You're declaring a variable $temp within the POST request. So it's basically to late to inject this variable - this needs to be done on the initial GET request where the resource (index.html containing your JS for example) is build.

You have two options here.

  1. Inject a token while serving the content. Since you should not use inline JS, you could include a hidden field containing your token / date to check in a later POST request.
  2. Get your token / date in advance to the POST request with a separate AJAX call.

General: You should not use plain text responses - especially if you are processing them with JS. Better encode your response as JSON, where you are able to give a structured response. See json_encode ( https://www.geeksforgeeks.org/php-json_encode-function/ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM