I have a JSON array that I'm passing to PHP. Eventually I'll be passing and receiving alot more from my PHP file but for the moment this is it. Right now it's recieving 1 array and sending back that same array, no problem. I can loop through the data in Javascript but how do I loop through the array I pass to my PHP file? I get errors with foreach and a for loop didn't seem to help any. Suggestions?
Javascript
var fullName = ["John Doe", "Jane Doe"];
$(window).load(function(){
getList();
});
function getList(){
$.getJSON(
"names.php",
{names : JSON.stringify(fullName)},
function(data)
{
for(var i = 0; i < data.test.length; i++)
{
window.alert(data.test[i]);
}
}
);
}
PHP
<?php
$names=json_decode($_REQUEST['names']);
foreach($names as $name)
{
echo $name;
}
$data['test'] = $names;
echo json_encode($data);
The foreach errors out on the foreach line telling me "Warning: Invalid argument supplied for foreach()"
$namesshould be an array. What's the problem with reading it?successin jquery just means that the script was called without any problems. It does not mean there are no errors in the script itself.