I’m trying to pass an array in an ajax request, but apparently it doesn’t work…
$.post("process.php", array,
function(data) {
document.getElementById("output").innerHTML = JSON.parse(data);
});
My question is, how to use the data sent in the process file?
The array is built like that: [key (0/1/2...)] => ["prod_id"]. The id varies.
I read somewhere using $_POST["key"]; would work, but it doesn’t.
It’d be even better if I could just get the array as is in the process file.
process.php (really basic – just to check wether it’s working or not.):
<?php
print($_POST["test"]);
?>
In order to receive data in php you need to send key/value pairs, however you are only sending a value.
You receive in php with
$_POST[key]which will return the value for that key.JS:
php
To return this array from php as text just to test your ajax can use
var_dump( $_POST)orvar_dump($array);If you intend to receive JSON in response from server, you do not need to use JSON.parse , jQuery will parse json internally. However you would need to add “json” as dataType argument to $.post