If you would like to would like to send data from php to javascript down, you can use json_encode.
To recieve data with php you can use $_GET and $_POST (as long as you are writing an application in which all that is simple) :)
For your Ajax Requst you (I allow this just for the author of this question) can use my javascript code:
function getRequestObject(){
var o = null;
if(window.XMLHttpRequest){
o = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
o = new ActiveXObject('Msxml2.XMLHTTP');
}catch(e1){
try{
o = new ActiveXObject('Microsoft.XMLHTTP');
}catch(e2){
}
}
}
return o;
}
function request(method, adress,sendData,callback){
var o = getRequestObject();
var async = (callback!==null);
if(method === 'GET'){
if(sendData!=null){adress+="?"+sendData;}
o.open(method, adress, async);
o.send(null);
}else if(method === 'POST'){
o.open(method, adress, async);
o.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
o.send(sendData);
}
if(async){
o.onreadystatechange = function (){
if(o.readyState==4&&o.status==200){
callback(o.responseText);
}else if(o.readyState==4&&o.status!=200){
//Error
}
};
}
if(async){return ;}
else{return o.responseText;}
}
The RequestCache is implemented as Object (look how this is done in Javascript)
But maybe JQuery or so can solve your tasks too.
function RequestCache (){}
RequestCache.cache = Array();
RequestCache.getRequest=function (method, adress,sendData,callback,enforceReload){
if(enforceReload===null){enforceReload=false;}
var url = method+adress+sendData;
var newUrl = true;
if(typeof(enforceReload)==="undefined"||enforceReload===false){
for(var key in RequestCache.cache){
if(key===url){
newUrl=false;
break;
}
}
}
if(newUrl){
if(callback){
request(method, adress,sendData,
function(res){
RequestCache.cache[url]=res;
callback(res);
}
);
}else{
RequestCache.cache[url]=request(method, adress,sendData,null);
return RequestCache.cache[url];
}
}else{
if(callback){
callback(RequestCache.cache[url]);
}else{
return RequestCache.cache[url];
}
}
};
RequestCache.setRequest = function (method, adress,sendData,result){
var url = method+adress+sendData;
RequestCache.cache[url] = result;
};