I have a jQuery UI datepicker. I want to send data to that function. I used .post function to fetch data from database as "data" through a PHP file. I am unable to access the data as the data returned to inside the .post function. I want it to the out side variable dat so that I can use it. Actually the data are the dates which are to be disable.
My code:
$(function () {
var dat = "";
var unavailableDates = ["10-5-2013", "14-5-2011", "15-5-2011"];
var dt = "id=1";
$.post("fetch.php", dt, function (data) {});
dat = data;
function enableFirday(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
var day = date.getDay();
return [((day == 6 || day == 5 || day == 3) && $.inArray(dmy, unavailableDates) < 0), ''];
}
$("#d").datepicker({
beforeShowDay: enableFirday
});
});
Inside the function there is a variable unavailableDates in the 3rd line. I want to fetch some date from my database and keep in that variable.
I thought I can use .post to fetch data from database so that I can use. But data are returned from PHP file and not copying to the variable dat. That are returned from PHP file can be used inside the .post function but not out side. How can I get the data to outside of the .post function. Please help me for this. It is very necessary to fetch data in my project.
getElementbyIdto fetch data tojavascriptvariable.function(data) {}only scopes thedatavariable to that empty block you defined for the function and so the following assignment ofdat = datawill always evaluate todat = undefinedunless you setdatasomewhere globally (which is still not a good thing). @MarcB's answer says the root of your issue best I just wanted to point that out.