我无法使用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).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代码
<?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
}
?>
试试这个:
JAVASCRIPT
var mainString = "code="+Code+"&abt_prt="+valueZero+"&InOut="+value;
在AJAX中
data : mainString
PHP
$code = $_POST['code']; //variable
$absent_present = $_POST['abt_prt']; //variable
$session = $_POST['InOut']; //variable
使用类似的数据
data: { code:Code , abt_prt : valueZero , InOut : value },
在php中,我真的不知道$_REQUEST是什么,但你可以使用
if(isset($_POST)){
}
尝试将数据变量更改为:
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');
}
});
但是,如果出于某种原因,您想像以前那样发送字符串,请使用json_decode对其进行解码。