I'm using the following ajax:
$.ajax({
type: 'POST',
url: '/search/search.php',
crossDomain: true,
data: {data: data},
dataType: 'json',
async: false,
success: function (response){
if (response.success)
{
$('#search-results').show();
for(field in response.data){
error = response.field_errors[field];
var name = field.name;
var barcode = field.barcode;
var serial = field.serial;
$("#searchname").html(name);
$("#searchbarcode").html(barcode);
$("#searchserial").html(serial);
}
}
else {
console.log("fail");
}
},
});
I'm trying to loop through the rows returned from the php, and put each row as a row in the table of my html.. I get the correct response from the php, but my loop doesn't show anything in the html.
HTML Table
<table class="table" id="search-results" style="display:none;">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Serial</th>
<th>Barcode</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td id="searchname"></td>
<td id="searchbarcode"></td>
<td id="searchserial"></td>
</tr>
</tbody>
</table>
PHP
$json = array();
if($num > 0)
{
$json['success'] = TRUE;
$i = 0;
while ($row = mysql_fetch_array($sql))
{
$json[$i]['data']['name'] = $row['name'];
$json[$i]['data']['barcode'] = $row['barcode'];
$json[$i]['data']['serial'] = $row['serial'];
$i++;
}
}
else
{
$json['success'] = FALSE;
}
echo json_encode($json);
console.log(field);give you in the loop?var name = field.name;infor..indoesn't make sense. You probably needvar name = response.data[field].name;. And the same forbarcodeandserial.