Reputation: 10635
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()"
Upvotes: 3
Views: 1704
Reputation: 76619
/* client-side */
$.ajax({
/* the request's method: */
type: 'GET',
/* the request's location: */
url:'/names.php',
/* the request's fields: */
data: JSON.stringify({names: fullName}),
/* the request's content-type : */
contentType: 'application/json; charset=utf-8',
/* the response's content-type: */
dataType:'json',
/* the callback function */
success: function(json){
if(json.length > 0){
$.each(json, function(i, v){
console.info(v);
});
}
else {
alert('wtf?!');
}
});
/* server-side */
$req=array_merge($_GET, $_POST);
// die(print_r($req));
$names=json_decode($req['names']);
header('content-type: application/json; charset=utf8;');
echo json_encode($names);
Alternatively one could set the request-method for the request, I'm just a little lazy and with that array_merge() it just doesn't matter if the request was a POST or GET (well, function $.ajax defaults to GET). Best practice is to use FireBug and checkout it's net-panel, especially the XHR tab. Probably the question should have been: "How to use a XHR debugger?" because there's nothing tricky about this at all.
It seems that the jQuery API has been slightly updated (see the Deprecation Notice):
http://api.jquery.com/jQuery.ajax/
Upvotes: 0
Reputation: 14149
json_decode()
doesn't return an array. To get it to do so you'd need to do json_decode($_REQUEST['names'], true)
http://php.net/manual/en/function.json-decode.php
Upvotes: 1
Reputation: 166
Try this, In your javascript function replace below line with
{names : JSON.stringify(fullName)},
with
"{names :"+ JSON.stringify(fullName) +"}",
debug test
var fullName = ['John Doe', 'Jane Doe'];
console.log({names : JSON.stringify(fullName)});
// gives Object { names="["John Doe","Jane Doe"]"}
but with
console.log("{names :"+ JSON.stringify(fullName) +"}");
// gives {names :["John Doe","Jane Doe"]}
// so i guess second is correct json string to pass
Upvotes: 0
Reputation: 1454
Try
$names=json_decode($_POST['names']); // or $_GET
instead of
$names=json_decode($_REQUEST['names']);
Sometimes $_REQUEST can be empty because of php.ini conf. SEE request_order
Upvotes: 0
Reputation: 8770
EDIT: jQuery is being insane again.
$.getJSON("names.php?names=" + encodeURIComponent(JSON.stringify(fullName)), function(data)
{
for(var i = 0; i < data.test.length; i++)
{
window.alert(data.test[i]);
}
}
);
The problem was it was adding each item in the array as seperate value to URI.
Upvotes: 0
Reputation: 91734
There seems to be something strange going on when using:
{names : JSON.stringify(fullName)}
As you are sending key - value pairs using GET
or POST
, you probably need to encode the value correctly.
You could do that using:
{names : encodeURIComponent(JSON.stringify(fullName))}
Upvotes: 0