I am passing an array from Javascript to PHP page, as below.
var arrF1 = [{"Item":"item1no",
"Desc":"item1desc",
"Remarks":"item1note"},
{"Item":"item2no",
"Desc":"item2desc",
"Remarks":"item2note"}
];
$.ajax({
type: "POST",
url: "http://www.mydomain.com/pdfs/flist.php",
data: { favArray : arrF1 },
success: function() {
alert('ok, sent');
}
});
In my PHP page, I read the array as below.
$fArray = json_decode($_POST['favArray'])
And I tried to access the arrays value like this.
$fArrav[0]->{'Item'}
$fArrav[0]->{'Desc'}
$fArrav[1]->{'Item'}
Is this correct?
I am generating a PDF on the server using FPDF.
But with the above, its not reading the array.
I must not be doing this right.
Please help.
Thank you.
With this PHP:
..and this Javascript:
…I get this output:
In this case, the encoding of the data on the wire is not JSON. It is ‘application/x-www-form-urlencoded’ as per the default used by jQuery’s
ajaxfunction. Looks like this:(all on one line)
Therefore it does not make sense to call
json_decodewithin PHP – there was never any JSON involved. PHP decodes the url-encoded post body automatically.If you want to encode with JSON, then you can use
JSON.stringify()directly. This may requirejson2.json the browser side. (see this answer)To use JSON, then you would need something like this in the browser:
…and then something like this on the php side:
In this case the on-the-wire representation looks like this:
…and the php var_dump output is this: