I'm using getJSON to retrieve information from a header record and the corresponding detail records and display the detail records in a table. The PHP script that is called returns a JSON string (via json_encode) that looks like this:
{
-
"srn":"1",
-
"invNo":"1",
-
"custNo":"39750",
-
"purchOrd":"PO12345",
-
"shipVia":"our truck",
-
"jobNo":"E001-11"
,
-
"invDate":"8/3/2011",
-
"taxCode":"1000000",
-
"shpName":"testName",
-
"shpAddr1":"test address 1",
-
"shpAddr2":"test address 2",
-
"shpCity"
:"Montgomery",
-
"shpState":"AL",
-
"shpZip":"36104",
-
"custName":"CUSTOMER NAME",
-
"custAddr1":"1234
HWY 456",
-
"custAddr2":"",
-
"custCity":"MONTGOMERY",
-
"custState":"AL",
-
"custZip":"36108",
-
"row":{
-
"1":{
-
"seqNo"
:"1",
-
"itemNo":"TEST2",
-
"itemDesc":"test item 2",
-
"shpQty":"0",
-
"invQty":"0",
-
"ordQty":"24"
,
-
"uom":"",
-
"prc":"24.00",
-
"total":"576.00"
-
},
-
"2":{
-
"seqNo":"2",
-
"itemNo":"TEST3",
-
"itemDesc":"test item 3"
,
-
"shpQty":"0",
-
"invQty":"0",
-
"ordQty":"53",
-
"uom":"",
-
"prc":"35.00",
-
"total":"1855.00"
-
}
-
}
}
Here is the html table:
- <table>
- <thead>
- <tr>
- <th colspan="2" >Item Description</th>
- <th>Quantity</th>
- <th>U/M</th>
- <th>Item Price</th>
- <th>Item Amount</th>
- </tr>
- </thead>
- <tbody>
- <tr class="p_row_1">
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- </tr>
- <tr class="p_row_2">
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- </tr>
- <tr class="p_row_3">
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- </tr>...
...And here is the $.getJSON:
- $.getJSON("GET_INV_INFO.php",{srn:'<?php echo $srn; ?>'},function(s){
- // Get Header Info
- $('#ip_invNo').html(s.jobNo);
- $('#ip_invDate').html(s.invDate);
- $('#ip_custNo').html(s.custNo);
- $('#ip_purchOrd').html(s.purchOrd);
- $('#ip_custName').html(s.custName);
- $('#ip_shipVia').html(s.shipVia);
- $('#ip_jobNo').html(s.jobNo);
- $('#ip_taxCode').html(s.taxCode);
- $('#ip_shpName').html(s.shpName);
- $('#ip_shpAddr1').html(s.shpAddr1);
- $('#ip_shpAddr2').html(s.shpAddr2);
- $('#ip_shpCity').html(s.shpCity+', ');
- $('#ip_shpState').html(s.shpState+' ');
- $('#ip_shpZip').html(s.shpZip);
- $('#ip_custName').html(s.custName);
- $('#ip_custAddr1').html(s.custAddr1);
- $('#ip_custAddr2').html(s.custAddr2);
- $('#ip_custCity').html(s.custCity+', ');
- $('#ip_custState').html(s.custState+' ');
- $('#ip_custZip').html(s.custZip);
-
- // Get Detail Info
- for(var i=0; i<31; i++){
- if(s.row.length <= i){
- //POPULATE TABLE WITH RECORD
- } else {
- //INSERT EMPTY ROW INTO TABLE
- }
- }
- });
My problem comes in the getJSON section where I'm trying to populate the table with a record. I don't know how to refer to the different fields in array 's'. I've tried the following and many variations, but to no avail:
$('.p_row_'+i+':nth-child(1)').html(s.row.i.itemNo);
$('.p_row_'+i+':nth-child(1)').html(s.row[i].itemNo);
How do I refer to a nested field in a multidimensional array like this?