I used this example https://stackoverflow.com/a/7601415/2118559
However I can not get it to work correctly
At first in outside php file have such array $fetch_child_topic
Array
(
[TopicName] => A
[TopicUrl] => a
)
In the outside PHP file I convert it to JSON
echo json_encode($fetch_child_topic, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
In the 'main file' the JavaScript looks like this {"TopicName":"A","TopicUrl":"a"} its name is data.
And here trying to create drop down menu
for (var text in data) {
alert( 'text ' + text + ' data' + data );
var val = data[text];
$('<option/>').val(val).text(text).appendTo($('#first_subcat_id'))
};
Drop down menu get long list with numbers like 0,1,2... but I would expect to see the letter A.
In the alert I see
text 0 data{"TopicName":"A","TopicUrl":"a"}
text 1 data{"TopicName":"A","TopicUrl":"a"}
....
text 31 data{"TopicName":"A","TopicUrl":"a"}
Something is wrong... but I can't understand what.
Any ideas, please?
EDIT
code in php file is like this (code a bit modified, to have only one column)
Get data from mysql
try {
$stmt = $db->prepare("SELECT TopicName FROM $table_name WHERE UpperTopicName = ?");
$stmt->execute( array($_POST['main_topic']) );
$fetch_child_topic = $stmt->fetchAll(PDO::FETCH_COLUMN);
}//try {
catch (PDOException $e){
echo "<br> Url DataBase Error: " .htmlspecialchars( $e->getMessage() , ENT_QUOTES, "UTF-8").'<br>';
}
catch (Exception $e) {
echo " Url General Error: ".htmlspecialchars( $e->getMessage() ).'<br>';
}
and
echo json_encode($fetch_child_topic, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
Solution
after get data from external php file (name of jquery var is data), did this
var data = $.parseJSON( data );
$.map( data, function( val, i ) {
$('<option/>').val(i).html(val).appendTo('#first_subcat_id');
});
In drop down menu get letters A, B (as expected)