I am trying to do a small web tool. Therefore I want to get a list of categories from a Mysql database via a Ajax request in Jquery. Unfortunately my Jquery function which should return an array of categories, returns only "undefined".
Jquery function (the returned data from the database are correct)
function get_categories($categoryname) {
var values =[];
$.ajax({
url: '../php/get_categories.php',
method: 'POST',
data: {categoryname: $categoryname},
success: function (data)
{
values = JSON.parse(data);
return values;
}
});
}
Button Event using the request:
function update_categories() {
var list = "<ul>";
alert(get_categories("%"));
var result = get_categories("%");
$.each(result, function (index, value)
{
list += "<li>" + value + "</li>";
});
list += "</ul>";
$("#category_list").html(list);
}
When showing the result of the request (data), the proper categories are listed, but when I show the returned value (result), it displays "undefined".
Can anyone tell me what I am doing wrong?