I have a form with 17 checkboxes. When one of these changes, JavaScript should submit the values to the server (running PHP).
I want to use JSON, because the checkboxes give two arrays which have to be seperated in PHP
In JS, I create an JSON-String, which I want to submit via POST and read and decode in PHP.
The String looks like this atm: [["2015-06-26","2015-06-27"],["2","3","4","5","6","7","8","9","10","11","12","13","14"]]
- This is what I want it to be.
This is, what my AJAX-function looks like:
var fullArray = [dateArray, trackArray];
var jsonFullString = JSON.stringify(fullArray);
//jsonFullString == [["a","b","c"],["d","e","f","g"]]
$.ajax({
type:'POST',
url:'shownitems.php',
data: jsonFullString,
success: function(data){
//More script. This comment is reached, because
alert(data);
// works.
}
});
When I get it to PHP, and search for $_POST[0]
the success function in JS doesn't show anything. When I search for $_POST
, I get "Array.." back.
This is, what my PHP looks like (This is my test snippet):
<?php
echo $_POST;
echo ".";
echo $_POST[0];
echo ".";
echo $_POST[0][0];
$array = array();
?>
I am also using jQuery.
var_dump()
function to print variable content for debugging. There is no $_POST[0], andecho $_POST
will print "Array" because it is an array - you'll only ever know what is inside withvar_dump($_POST);
array(0){}
... I'm not able to get the content of the arrays ... Even with json_decode(); ...[["2015-06-26","2015-06-27"],["2","3","4","5","6","7","8","9","10","11","12","13","14"]]
, the post request payload must be strictly key-value pairs.data: 'jsonFullString='+jsonFullString,
then look for (andjson_decode
)$_POST['jsonFullString']
data: {data: fullArray}
without any explicit encodings-decodings anywhere (not sure why people prefer to encode things twice for no real reason)