Y’all,
Been playing with javascript and trying to create JSON object and send to php via post. No luck though. Here is what I am doing and for some reason I cant iterate through the object in php. Please help.
Creation of array of objects: (javascript)
var jObjArr = new Array();
function addItem(name, selNum, selCost, selQty, selSize) {
var tempObj = {};
tempObj.prodName = name;
tempObj.catNum = selNum;
tempObj.prodCost = selCost;
tempObj.prodQty = selQty;
tempObj.prodSize = selSize;
jObjArr.push(tempObj);
}
Once user is ready I call this method to POST to a separate php page
function sendIt() {
var JSONstr = JSON.stringify(jObjArr);
document.getElementById("orderValue").setAttribute("value", JSONstr);
}
Now after that I am trying to iterate through the sent $_POST data like so. It comes back properly as; [{“prodName”: “T-Shirt White”, “catNum”: “1”, “prodCost”: 5, “prodQty”: 2, “prodSize”: 2}, {“prodName”: “Briefs White”, “catNum”: “5”, “prodCost”: 7, “prodQty”: 2, “prodSize”: 3}].
But I cant get at each item.
$jsonStr = $_POST['order'];
$decoded = json_decode($jsonStr, true);
echo $decoded; //result [{"prodName": "T-Shirt White", "catNum": "1", "prodCost": 5, "prodQty": 2, "prodSize": 2}, {"prodName": "Briefs White", "catNum": "5", "prodCost": 7, "prodQty": 2, "prodSize": 3}]
foreach ($decoded->prodName as $prod) {
echo $prod . " something "; //Shows nothing
}
I have tried looping through or just even
echo $decoded.prodName; //Nothing
echo $decoded->prodName; //Nothing
echo $decoded[‘prodName’]; //Nothing
Any ideas what I am doing wrong?
Thanks in advance MT