I've got an AJAX request in my index.php to another file (get_code.php) that queries a DB and returns a json_encoded array to index.php, where it is converted to a string with JSON_stringify.
This works fine, but for the fact that it comes out on the page like so:
[{"code":"GG500","desc":"Desc 1","price1":"9.35","price2":"8.25","price3":"7.75","avg":"7.994198","lwcm":"7.9568","cost":"4.63"},
{"code":"GGC4","desc":"Desc 2","price1":"","price2":"","price3":"","avg":"504.666666","lwcm":"387.5","cost":"260.61"}]
Question:
How can I get this string and convert it into a normal array or whatever that I can display in an HTML table in index.php?
Current code:
index.php jQuery
if (!data.result) {
$('#modal2').modal('show');
$('.msgs').text(data.error);
$('#cdnf').text(data.code);
$('#tarray').text(JSON.stringify(data.tarray));
}
index.php HTML
<p id="tarray"></p><!-- needs to be a table -->
get_code.php PHP
$taq = mysql_query("SELECT * FROM variants")or die(mysql_error());
if($taq){
$tarray = array();
while($row = mysql_fetch_assoc($taq)){
$tarray[] = array(
'code' => $row['va_code'],
'desc' => $row['va_description'],
'price1' => $row['price_1'],
'price2' => $row['price_2'],
'price3' => $row['price_3'],
'avg' => $row['average_price'],
'lwcm' => $row['lwcm'],
'cost' => $row['cost']
);
}
};
die(json_encode(['tarray' => $tarray]));
Unexpected token o in JSON at position 1?