PHP
$table = mysql_real_escape_string($_REQUEST['table']);
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = '$table'";
$result = mysql_query($query);
$arr1 = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arr1[] = $row['COLUMN_NAME'];
}
echo json_encode($arr1);
}
Javascript/Jquery
$("#verticalSelect").change(function() {
var table = $("#verticalSelect option:selected").attr('value');
$.post(PROCESSORFILE, {"task": "getTableDupeFields", "table": table}, function(data) {
$.each(data, function(key, value){
$("#test-area").append(key+' is: '+value+'<br/>');
});
alert(data);
});
});
Now with the alert of the returned JSON variable I get the result I expect.
["lead_id", "callcenter", "monkey" ...]
But with the Jquery.each() function I get this:
0:is: [
1:is: “
2:is: l
3:is: e
4:is: a
5:is: d
6:is: _
7:is: i
8:is: d
9:is: “
10:is: ,
11:is: “
12:is: c
13:is: a
14:is: l
15:is: l
16:is: c
17:is: e
18:is: n
19:is: t
20:is: e
21:is: r
Every character of the returned array is looped instead of every discreet value. What is the proper way of handling JSON returned arrays?
You either need to set your response header to
application/jsonor specify the dataType in$.postto be ‘json’