I am trying to pass a json file to PHP using Jquery. when I check the response I get the error json_decode expects parameter 1 to be a string.
Here is my jquery routine
$("#ProcessData").click(function(){
var cols = $("#tblGroup thead tr th").map(function(){
return $(this).text(); });
var headers = cols;
// Fetch the data from the table body
var tableObject = $("#tblGroup tbody tr.tableClass").map(function(i){
var row = {}; $(this).find('td').each(function(i){
var rowName = headers[i];
row[rowName] = $(this).text();
});
return row;
}).get();
// convert object to JSON
JSON.stringify(tableObject);
//now call ajax to pass info to php
$.ajax({
url: 'php/ProcessOrder.php',
data: {my_json_data: tableObject},
type: 'POST',
async: false,
dataType: 'json', cache:false
});
});
here is my php script
<?PHP
//this is the layout of the json object
//Title:First Name:Surname:Group Name this will needamending as the json object builds
require("dbsettings.php");
$_Reference= $_POST["my_json_data"]; //this is a json object
// Loop through Array
$someArray = json_decode($_Reference, true); // Replace ... with your PHP Array
foreach ($someArray as $key => $value) {
echo $value["Title"] . ", " . $value["First Name"] . ", " . $value["Surname"] . ", " . $value["Group Name"] . "<br>";
}
?>
here is my json object
{"Title":"Mr","First Name":"12","Surname":"12","Group Name":"as"}
Using php fiddle I created and tested this script which worked perfectly
<?php
$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
// Loop through Array
$someArray = json_decode($someJSON, true); // Replace ... with your PHP Array
foreach ($someArray as $key => $value) {
echo $value["name"] . ", " . $value["gender"] . "<br>";
}
?>
One thing I did notice is my php fidle json file has [] the posted object doesn't is this what the error message refers too? Or Should I not JSON.stringfy() and just pass it as a string into php and use JSON_encode?
thanks for any assistance