I'm fetching info from the database (PHP/MySQL), and send the result (via AJAX) into a JSON object. I'm collecting the result into an array. When I try to display the content of the array in some inputs, I have an "undefined" result. But when I console my array, everything is here! I'm not sure I understand the process... Would somebody please help ? (I hope I'm not repeating another question I didn't find...) THANK YOU!
function exportData(dbname, line) {
var tab = [];
var content;
$.ajax({
type: "post",
url: "myPHPfile.php",
dataType: "json",
data: { dbname: dbname, line: line },
success: function(data) {
for (var i=0; i < data.length-1; i++) {
data.forEach(function (a) {
Object.keys(a).forEach(function(key) {
a[key] = Number(a[key]) || a[key];
});
});
tab[i] = data[line][i];
}
},
error: function (req, stat, err) {
console.log("Reading PHP Error: " + req.responseText);
}
});
for (var k=0; k < tab.length; k++) {
content += "<input type='text' name='" + tab[k] + "' value='" + tab[k] + "'/>";
$("#mywindow").append(content);
}
console.log(tab); //Gives a nice result of the content of the array
console.log(tab[0]); //Gives "undefined"!
}