我正在使用jQuery AJAX将数组发送到PHP脚本,我的jQuery代码如下:
$.ajaxSetup({
cache: false
});
$.ajax({
type: "POST",
data: {
row_ids : row_ids//This is the array sent
},
url: "CRUD.php",
success: function (response) {
alert(response);
},
error: function () {
alert("Server side error, try later!");
}
});
获取数组的PHP代码如下:
<?php
//Working code :
$ids = $_POST['row_ids'];
print_r($ids);
//Not working code :
$ids = filter_input_array(INPUT_POST, 'row_ids');
print_r($ids);
?>
通过AJAX向PHP发送的简单变量filter_input(INPUT_POST, 'variable_passed');
对我来说工作正常。
有人可以告诉我为什么它不适用于filter_input_array?
感谢。
答案 0 :(得分:0)
您使用此功能错误。请阅读php文档http://php.net/manual/en/function.filter-input-array.php
中如何使用此功能第二个参数必须是带有验证定义的数组
使用示例:
<?php
error_reporting(E_ALL | E_STRICT);
/* data actually came from POST
$_POST = array(
'product_id' => 'libgd<script>',
'component' => '10',
'versions' => '2.0.33',
'testscalar' => array('2', '23', '10', '12'),
'testarray' => '2',
);
*/
$args = array(
'product_id' => FILTER_SANITIZE_ENCODED,
'component' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_range' => 1, 'max_range' => 10)
),
'versions' => FILTER_SANITIZE_ENCODED,
'doesnotexist' => FILTER_VALIDATE_INT,
'testscalar' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
),
'testarray' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_ARRAY,
)
);
$myinputs = filter_input_array(INPUT_POST, $args);
var_dump($myinputs);
echo "\n";
?>