如何在PHP中访问AJAX响应变量
我无法使用php通过ajax访问变量 AJAX代码如何在PHP中访问AJAX响应变量,php,jquery,ajax,Php,Jquery,Ajax,我无法使用php通过ajax访问变量 AJAX代码 $("input[name='absent[]'").change(function() { var obj = $(this); //checkbox var valueZero = obj.val(); var Code = obj.attr('data-Code'); var value = obj.attr('data-session'); /*var theTR = $(this).paren
$("input[name='absent[]'").change(function() {
var obj = $(this); //checkbox
var valueZero = obj.val();
var Code = obj.attr('data-Code');
var value = obj.attr('data-session');
/*var theTR = $(this).parent('tr').children().find('td:eq(0)').addClass('hidden');*/
/* alert( theTR.text());*/
/*$(this).addClass('hidden');*/
$.ajax({
data: "{ code: '"+ Code +"', abt_prt: "+ valueZero +", InOut: "+ value +" }", // need to access these variables in php
type: "post",
dataType:'json',
url: "insertabsent.php",
success: function(){
obj.addClass('hidden');
}
});
});
<?php
if(isset($_REQUEST))
{
$code = $_POST['code']; //variable
$absent_present = $_POST['abt_prt']; //variable
$session = $_POST['InOut']; //variable
//need this variables to perform a insert query
}
?>
PHP代码
$("input[name='absent[]'").change(function() {
var obj = $(this); //checkbox
var valueZero = obj.val();
var Code = obj.attr('data-Code');
var value = obj.attr('data-session');
/*var theTR = $(this).parent('tr').children().find('td:eq(0)').addClass('hidden');*/
/* alert( theTR.text());*/
/*$(this).addClass('hidden');*/
$.ajax({
data: "{ code: '"+ Code +"', abt_prt: "+ valueZero +", InOut: "+ value +" }", // need to access these variables in php
type: "post",
dataType:'json',
url: "insertabsent.php",
success: function(){
obj.addClass('hidden');
}
});
});
<?php
if(isset($_REQUEST))
{
$code = $_POST['code']; //variable
$absent_present = $_POST['abt_prt']; //variable
$session = $_POST['InOut']; //variable
//need this variables to perform a insert query
}
?>
使用如下数据
data: { code:Code , abt_prt : valueZero , InOut : value },
在php中,我不知道$u请求是什么,但您可以使用
if(isset($_POST)){
}
像这样使用数据
data: { code:Code , abt_prt : valueZero , InOut : value },
在php中,我不知道$u请求是什么,但您可以使用
if(isset($_POST)){
}
尝试将数据变量更改为:
data: {"code":Code,"abt_prt":valueZero,"InOut":value},
尝试将数据变量更改为:
data: {"code":Code,"abt_prt":valueZero,"InOut":value},
您误解了必须如何发送AJAX参数。您不需要发送索引,您可以发送一个简单的Javascript对象,如下所示:
$.ajax({
data: { code: Code, abt_prt: valueZero, InOut:value}, // need to access these variables in php
type: "post",
dataType:'json',
url: "insertabsent.php",
success: function(){
obj.addClass('hidden');
}
});
但是,如果出于某种原因,您希望像以前那样发送字符串,那么请使用进行解码。您误解了必须如何发送AJAX参数。您不需要发送索引,您可以发送一个简单的Javascript对象,如下所示:
$.ajax({
data: { code: Code, abt_prt: valueZero, InOut:value}, // need to access these variables in php
type: "post",
dataType:'json',
url: "insertabsent.php",
success: function(){
obj.addClass('hidden');
}
});
但是,如果出于某种原因,您希望像以前那样发送字符串,请使用进行解码。请尝试以下操作:
JAVASCRIPT
var mainString = "code="+Code+"&abt_prt="+valueZero+"&InOut="+value;
data : mainString
AJAX中的
var mainString = "code="+Code+"&abt_prt="+valueZero+"&InOut="+value;
data : mainString
PHP
$code = $_POST['code']; //variable
$absent_present = $_POST['abt_prt']; //variable
$session = $_POST['InOut']; //variable
请尝试以下方法:
JAVASCRIPT
var mainString = "code="+Code+"&abt_prt="+valueZero+"&InOut="+value;
data : mainString
AJAX中的
var mainString = "code="+Code+"&abt_prt="+valueZero+"&InOut="+value;
data : mainString
PHP
$code = $_POST['code']; //variable
$absent_present = $_POST['abt_prt']; //variable
$session = $_POST['InOut']; //variable
什么是
print\r($\u POST)代码>?什么是打印($\u POST)代码>?插入查询正在执行,但变量仍然不可访问插入查询正在执行,但变量仍然不可访问如何解码?它会在php文件中完成吗?如果您按照我代码中最初的建议发送数据,那么您不需要对其进行解码。我如何对其进行解码?它会在php文件中完成吗?如果您按照我的代码中最初的建议发送数据,那么您不需要对其进行解码。