Hi Everyone.
I am using AJAX to make a call to a database that contains a few tables. In each table are 2 fields ('name' & 'sq').
While I have been successful in sending the 'name' data back using json_encode and manipulate it. I am struggling to send both fields back in a suitable manner that I can then easily manipulate the data contained.
The database table has a field 'name' which contain names and a field 'sq' which contains a corresponding number as below:
Thermal Liner 1
R300 Liner 2
Fibre Liner 3
Premium 4
This is as far as I have got with the code:
This is the ajax call I am using:
Code:
$(function () {
$.ajax({
type: "POST",
url: 'api6.php',
data: "postData=" + postData,
dataType: 'json',
success: function(data)
{
for(i=0; i<dataLength; i++) {
optionString += '<option value="'+data[i]+'">'+data[i]+'</option>';
}
selectString += '<select name="producttype"
id="producttype">'+optionString+'</select>';
$('#output').html(selectString);
}
This is the code on the api6.php page:
Code:
<?php
$host = "localhost";
$user = "root";
$pass = "root";
$databaseName = "calculator";
//$tableName = "calc";
$tableName = $_POST['postData'];
//include 'db_access.php';
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$result = mysql_query("SELECT * FROM $tableName");
//$db_data = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$db_data[] = array($row['name']);
$db_data2[] = array($row['sq']);
}
echo json_encode(array($db_data,$db_data2));
?>
THis code works fine for just passing back the single line $db_data[] = array($row['name']);, but I have added the extra code (in bold) to send back the extra data in the 'sq' field. The string coming back as two srings:
Thermal Liner,R300 Liner,Fibre Liner,Premium
1,2,3,4
How do I go about parsing the data so that I can separate out the data in each string so that it can be manipulated by javaScript or jQuery?
Any help, direction to a tut or other URL would be greatly appreciated.
Thanks for your time.