Hello. I'm working with PHP 5 here. I have the following code:
$data = json_decode($_POST['data']);
foreach ($data as $obj) {
......
}
I get the error "Invalid argument supplied for foreach()" on the line with the foreach function. This only happens on my shared server account. On my local webserver everything works fine. $_POST['data'] contains valid JSON string. print_r($data); shows nothing...What the hell is wrong here ?
EDIT: It really blows my mind but the $_POST['data'] string is being sent with AJAX and I catch the string with FireBug and copied into a JSON test file like this:
$data = json_decode('[{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"}]');
print_r($data);
And it comes out as it should, properly. So I'm gonna paste more code from the function that troubles:
function saveData($table)
{
$data = json_decode($_POST['data']);
$db = new MySQL(true);
$db->TransactionBegin();
foreach ($data as $obj) {
$id = $obj->id;
$name = $obj->name;
}
}
Check this out: If I do this:
$data = json_decode('[{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"},{"id":3,"name":"John","surname":"Smith","number":6633,"city":"The city","area":"West","street":"West","numar":"15","other":"none"}]');
foreach ($data as $obj) {
......
}
Works perfect! So there seems to be e problem when I do:
$data = json_decode($_POST['data']);
But $_POST['data'] outputs a perfect JSON string. It's driving me crazy.
Any ideas ?
Any ideas ?