I want to read the (images) files name inside a specific folder and then to preview images in Krajee Bootstrap FileInput (FileInput demo link) .
I have a problem with this code in javascript:
var initialPreviewArray = [];
var j = [];
$.ajax({
type: "POST",
url: "my_gallery_ajax.php?",
async: "FALSE",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// alert('ok');
$.each(data, function(i,filename) {
j[i] = filename;
initialPreviewArray.push("<img id='"+i+"' src='user_1/'"+j[i]+"'>");
});
alert(j[2]); // test - is working - value: '11.jpg'
}
});
alert(j[2]); // test - not working - value: 'undefined'
My goal is to fill 'initialPreviewArray' with data from AJAX, inside or outside.
Inside 'AJAX call' initialPreviewArray.push is not working, even with specific value (outside AJAX is working):
initialPreviewArray.push("<img style='height: 50px' id='1' src='user_1/1.jpg'>");
Conclusion: I am not able to get data outside AJAX.