Q:

access data from php to javascript

I intend to access set of records from MYSQL and handle it in Javascript for a calendar component. Hence I used PHP to fetch the records and dumped it as a array of json records to a flat file. Now i tried to read this json via flatfile from javascript but I am facing few issues. 
var event_list;
$.getJSON("testFile.json", function(json) {
 alert("fetching");
 event_list= jQuery.extend(true, {}, json);
 alert(json);
 event_list=json;
 alert(JSON.stringify(event_list));   // This echo's me the data
 });
 alert(JSON.stringify(event_list));   // But this doesn't ???

I am unable to access the data outside the scope of getJSON() routine.
It would be great if someone could help me with this snippet.

A:

You could make use of the async property and set to FALSE and global property to FALSE which would serve your requirements.
var data;
data= function () {
var tmp = null;
$.ajax({
    'async': false,
    'type': "POST",
    'global': false,
    'dataType': 'html',
    'url': "fetch_data.php",
    'data': { 'userid': "19"},
    'success': function (json) {
        tmp = json;
    }
});
return tmp;
}();

alert(data); //would give you the json data from PHP

