我想在前端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 成功回调函数,我该如何解决上述问题?
最佳答案
您在 POST 请求中声明变量 $temp
。所以注入(inject)这个变量基本上已经太晚了——这需要在构建资源(例如包含 JS 的 index.html)的初始 GET 请求上完成。
这里有两个选择。
- 在提供内容时注入(inject) token 。由于您不应该使用内联 JS,因此您可以包含一个包含 token /日期的隐藏字段,以便在以后的 POST 请求中 checkin 。
- 通过单独的 AJAX 调用提前获取 POST 请求的 token /日期。
一般:您不应该使用纯文本响应 - 特别是当您使用 JS 处理它们时。更好地将您的响应编码为 JSON,这样您就可以给出结构化响应。请参阅 json_encode ( https://www.geeksforgeeks.org/php-json_encode-function/ )。
关于javascript - 如何将 php 变量传递给 ajax success 函数并分配给 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59028685/