I'm trying to pass an object using jQuery post. It passes alright but something goes wrong with the php code:
} elseif($_GET['action'] == "insertQty") {
$obj = json_decode($_POST['param']);
$scArr = $obj->{'data'};
$od_id = $obj->{'id'};
for($i = 0; $i < count($scArr); $i++){
$pd_id = substr($scArr[$i], 5);
$pQty = $obj->{$scArr[$i]};
$stmt = $DBH->prepare("INSERT INTO tbl_order_item_test (od_id, pd_id, od_qty) VALUES (:oId, :pId, :pQty)");
$stmt->bindParam(':oId', $od_id);
$stmt->bindParam(':pId', $pd_id);
$stmt->bindParam(':pQty', $pQty);
$stmt->execute();
}
}
$scArr[$i]
= "prod_#" where # is some number. Therefore substr(5) = #
.
the obj:
var array = getArray();
var itemsObj = {};
itemsObj['data'] = shoppingCart;
itemsObj['id'] = data;
//for-loop here to dynamically insert data to the obj.
$.post(url, "param=" + JSON.stringify(itemsObj), "html");
What could not be working properly? It's most likely a problem with the first paragraph of code since the object comes out as it should be when using alert()
.
EDIT:
print_r($obj)
:
stdClass Object
(
[data] => Array
(
[0] => prod_2
)
[id] => 1024
[prod_2] => 8
)
You can pretty much guess the outpud of $scArr
from the above... Basically everything object-related is fine.