I want to get an array that I used with Ajax. I already used this method with an other page and this time it doesn't work, so I don't see where is the problem.
I have an array :
var array_downloads = <?php echo json_encode($array_downloads);?>;
This array isn't empty, when I do a console.log
I get the content, the list of the elements.
After that, I make my Ajax request like that :
$.post("pack.php", {arr:array_downloads}, function(data) {
...
},'json');
So I send my array to the file .php
.
Finally I want to get the array with this :
$array_downloads = $_POST["arr"];
And when I try to echo it (and I don't forget the json_encode()
), I get null
. I don't understand why. Help please !
I tries with the same code and it works as it is suppose to:
The only code I changed is wrapped
json_encode
output in quotes so thatjavascript
will consider it as a string.
main.php
<?php $array_downloads = [1, 2, 3, 4, 5]; ?>
<script>
var array_downloads = '<?php echo json_encode($array_downloads);?>';
$.post("pack.php", {arr: array_downloads}, function (data) {
console.log(data);
}, 'json');
</script>
pack.php
<?php
var_dump(json_decode($_REQUEST['arr'],true));
?>
Output in pack.php
array (size=5)
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5