I send a valid JSON string to my PHP page from jQueryscript:
var data = '{
"data":[
{
"id":"12",
"checked":"true"
},{
"id":"4",
"checked":"false"
},{
"id":"33",
"checked":"false"
}
]
}';
$.post ("page.php", { data_input:data }, function (data) {
// code
});
Once I get the data in my PHP page, I parse it with the json_decode
method, and then try to use it in a foreach
statement created for a PDO
query:
<?php
$data_input = json_decode ($_REQUEST['data_input'], true);
$sql = "UPDATE my_table SET user_enabled = :checked WHERE node_prop_id = :id";
$stmt = $dns->prepare ($sql);
foreach ($data_input as $data) {
$ok = $stmt->execute ($data);
$num = $stmt->rowCount ();
}
if ($ok) return 1;
else return 0;
?>
This returns me the error:
PHP Warning: Invalid argument supplied for foreach() in /home/.../page.php on line XX
Can I find a way to use my JSON data in the foreach
statement?