0

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?

3
  • 2
    Right now you are sending plain text as response from your PHP script. You should change that, so that you return a proper data structure - otherwise, you will have trouble taking your text and your variable value apart on the client side. Make that a proper JSON response that returns an object with proper keys, and handle it accordingly on the client side. Commented Nov 25, 2019 at 10:02
  • yes bro i am looking json method to do this task. Commented Nov 25, 2019 at 10:08
  • thank you gays task is completed .. Commented Nov 25, 2019 at 10:30

1 Answer 1

1

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/).

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.