I am sending data from javascript to my php server. But I can't access all my variables sent. There are some nested arrays and JSON strings and I just can't figure out how to access them.
Here my code:
sender.js
$scope.report = {
'title': '',
'desc': '',
'address': {
'text': '',
'lat': '',
'lng': ''
},
'tags': ['none chosen']
};
function postToServer () {
var data = {
'do': 'addNewReportToDatabase',
'data': {
'usr': userId,
'report': JSON.stringify($scope.report)
}
};
$http({
url: $rootScope.server_url,
method: "POST",
data: data,
headers: {'Content-Type': 'application/json'}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
alert(response);
});
}
server.php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$data = json_decode(json_encode($request->data), True);
if (isset($request->do)) {
switch ($request->do) {
case 'addNewReportToDatabase':
$reportStr = $data['report'];
$reportJSON = json_decode($reportStr);
echo $reportJSON['title'];
break;
}
}
I can't access any variables of my report-variable.
I CAN say echo json_encode($reportJSON);
and if I log it in my JS the response I get is an Object with all my variables as it's supposed to be!
The stringified version looks then like this: {"title":"title","desc":"describtion","address":{"text":"address","lat":"","lng":""},"tags":["Unfall","Terrorismus","Regional"]}
If I user var_dump($reportJSON);
I get the following output:
"object(stdClass)#3 (4) { ["title"]=> string(5) "title" ["desc"]=> string(11) "describtion" ["address"]=> object(stdClass)#4 (3) { ["text"]=> string(7) "address" ["lat"]=> string(0) "" ["lng"]=> string(0) "" } ["tags"]=> array(3) { [0]=> string(6) "Unfall" [1]=> string(11) "Terrorismus" [2]=> string(8) "Regional" } } "
So why can't I access the variables with my php??