PHP obect that contains an array of objects JavaScript JSON
I have a question regarding JSON and encoding objects. The below Catalog
object contains an array of objects amongst other things. Once the object
is constructed on the server side using an AJAX request, I would like to
pass the object back to JavaScript as a response, so I can further use it.
The following approach results in a ReferenceError: catalog is not defined
error. The object is successfully created on the server side, so I suppose
something went wrong with the way I am using json_encode. I appreciate any
suggestions.
Thnaks in advance!
JavaScript
$.ajax({
url: "invoice-get-data.php?loadCatalog=1",
dataType: "json",
success: function(catalog){
alert(catalog.item[2].name);
}
});
AJAX / PHP
$catalog = new Catalog();
json_encode((object) $catalog);
Catalog Object Structure
public $item = Array();
public $itemCount;
//mySQL connection...
//populate catalog
foreach($results as $row){
$this->item[] = new Item($row['id'], $row['prod_name'], $row['price'],
$row['img_path']);
}
Item Object Structure
No comments:
Post a Comment