I have to develop a part of my project in javascript, which really isn't a language I feel confortable with.
Anyway, here's my problem : I have to work with the (json) data I get from the 'php/get_services_list.php' program (which is working well). So I am trying to store the data in my 'services' variable, but 'services' is destroyed as soon as I leave the getJson block
-> the first console.log(services); displays the json array, as expected
-> the second one just displays 'undefined', and of course I've got an error later when I try to evaluate services.length.
So yeah, I really need to be able to work with the data outside the $.getJSON block. Thanks in advance for your advices !
function addServiceInput() {
var services;
$.getJSON("php/get_services_list.php").done(function(data) {
services = data;
console.log(services);
});
console.log(services);
var select = "<select class='col-lg-3' name='service'>";
for (let i = 0; i < services.length; i++) {
select += "<option value='" + services[i] + "'>" + services[i] + "</option>";
}
select += "</select>";
return select;
}