I am passing data to an ajax call that has been 'json_stringified'. I am able to pass the data to PHP, however when trying to access the items within the passed array, I receive the following error:
'Trying to get property of non-object'
I've tried several things, but I am stuck. Can someone please assist? My code is below:
js:
$.ajax({
url: 'http://localhost/VibeSetter/services/getlocationsFavs.php',
dataType: 'json',
type: "POST",
data: {favs: JSON.stringify(localStorage.getArray("myFavs"))},
success: function(data, status)
{
$.each(data, function(i,item)
{
//pass to function to fill array
populateLocationsArray(i+1, item.idlocations,item.name,item.longitude, item.latitude);
});
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(arguments);
// alert('HTTP Error: '+errorThrown+' | Error Message:'+textStatus);
}
});
}
console.log(localStorage.getArray("myFavs")) returns:
{locations: 1},{locations: 0},{locations: 4293315}
php:
<?php
header('Content-type: application/json');
include 'config.php';
$data = json_decode($_POST['favs']);
var_dump($data);
foreach($data as $d)
{
print $d->locations;
}
?>
which returns the following in chrome developer tools pane:
array(3) {
[0]=>
string(14) "{locations: 1}"
[1]=>
string(14) "{locations: 0}"
[2]=>
string(20) "{locations: 4293315}"
}
Notice: Trying to get property of non-object in C:\xampp\htdocs\VibeSetter\services\getlocationsFavs.php on line 10
Notice: Trying to get property of non-object in C:\xampp\htdocs\VibeSetter\services\getlocationsFavs.php on line 10
Notice: Trying to get property of non-object in C:\xampp\htdocs\VibeSetter\services\getlocationsFavs.php on line 10
Can someone please tell me how to access each element of this array? The idea is to parse each 'location' value and concatenate the locations in a query further in the code as specific values in a query search.
Thanks in advance