Ti posto direttamente il codice della pagina che secondo me facciamo prima..
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
<script type="text/javascript">
function _getElementById(idElemento){
var elemento;
if(document.getElementById){
elemento = document.getElementById(idElemento);
}else{
elemento = document.all[idElemento];
}
return elemento;
}
function xmlhttpInstance(){
var xmlhttp = null;
if(window.XMLHttpRequest){// Mozilla, Safari, ...
xmlhttp = new XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){// IE
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Errore: Impossibile creare l'istanza XMLHTTP!");
}
}
}
return xmlhttp;
}
function ajaxDo(url,funzioneDone,div,asynchronous,method){
var
appendRandom = true,
appendRandomValue = "ajaxRnd",
massimaAttesa = 2;
var
useDegradableHref = true,
xmlhttp = xmlhttpInstance();
if(!xmlhttp){
return useDegradableHref;
}
var
useDegradableHref = false,
parameters = null,
getMethod = "get",
postMethod = "post",
urlParam = url.split("?"),
url = urlParam[0],
dataChiamata = new Date(),
inizioChiamata = dataChiamata.getTime(),
verificaTempoTrascorso;
if(asynchronous == undefined) asynchronous = true;
if(method == undefined) method = getMethod;
if(method == getMethod){
if(urlParam[1] != undefined){
url = url+"?"+urlParam[1];
}
if(appendRandom){
if(urlParam[1] != undefined){
url = url+"&"+appendRandomValue+"="+Math.random();
}else{
url = url+"?"+appendRandomValue+"="+Math.random();
}
}
}
if(method == postMethod){
if(urlParam[1] != undefined){
parameters = urlParam[1];
}
if(appendRandom){
if(urlParam[1] != undefined){
parameters = parameters+"&"+appendRandomValue+"="+Math.random();
}else{
parameters = appendRandomValue+"="+Math.random();
}
}
}
xmlhttp.open(method, url, asynchronous);
xmlhttp.setRequestHeader("connection", "close");
if(method == postMethod) xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
if(asynchronous) xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState === readyState.COMPLETATO){
verificaTempoTrascorso = function(){};
if(statusText[xmlhttp.status] === "OK" || statusText[xmlhttp.status] === "Not Modified"){
if(typeof(funzioneDone) === "function") funzioneDone.call(this, xmlhttp.responseText, div);
}else{
alert("Errore:\n" + xmlhttp.statusText);
}
}else if(massimaAttesa < 1000){
massimaAttesa = massimaAttesa * 1000;
verificaTempoTrascorso = function(){
dataChiamata = new Date();
if((dataChiamata.getTime() - inizioChiamata) > massimaAttesa){
xmlhttp.onreadystatechange = function(){return;};
xmlhttp.abort();
alert("Spiacente, richiesta fallita.\nLa prego di ritentare tra qualche istante.");
}else{
setTimeout(verificaTempoTrascorso, 100);
}
};
verificaTempoTrascorso();
}
};
try{
xmlhttp.send(parameters);
if(!asynchronous){
if(typeof(funzioneDone) === "function") funzioneDone.call(this, xmlhttp.responseText, div);
}
}catch (e){
xmlhttp.abort();
alert("Spiacente, richiesta fallita.\nLa prego di ritentare tra qualche istante.");
}
return useDegradableHref;
}
var readyState = {
INATTIVO: 0,
INIZIALIZZATO: 1,
RICHIESTA: 2,
RISPOSTA: 3,
COMPLETATO: 4
};
var statusText = new Array();
statusText[100] = "Continue";
statusText[101] = "Switching Protocols";
statusText[200] = "OK";
statusText[201] = "Created";
statusText[202] = "Accepted";
statusText[203] = "Non-Authoritative Information";
statusText[204] = "No Content";
statusText[205] = "Reset Content";
statusText[206] = "Partial Content";
statusText[300] = "Multiple Choices";
statusText[301] = "Moved Permanently";
statusText[302] = "Found";
statusText[303] = "See Other";
statusText[304] = "Not Modified";
statusText[305] = "Use Proxy";
statusText[306] = "(unused, but reserved)";
statusText[307] = "Temporary Redirect";
statusText[400] = "Bad Request";
statusText[401] = "Unauthorized";
statusText[402] = "Payment Required";
statusText[403] = "Forbidden";
statusText[404] = "Not Found";
statusText[405] = "Method Not Allowed";
statusText[406] = "Not Acceptable";
statusText[407] = "Proxy Authentication Required";
statusText[408] = "Request Timeout";
statusText[409] = "Conflict";
statusText[410] = "Gone";
statusText[411] = "Length Required";
statusText[412] = "Precondition Failed";
statusText[413] = "Request Entity Too Large";
statusText[414] = "Request-URI Too Long";
statusText[415] = "Unsupported Media Type";
statusText[416] = "Requested Range Not Satisfiable";
statusText[417] = "Expectation Failed";
statusText[500] = "Internal Server Error";
statusText[501] = "Not Implemented";
statusText[502] = "Bad Gateway";
statusText[503] = "Service Unavailable";
statusText[504] = "Gateway Timeout";
statusText[505] = "HTTP Version Not Supported";
statusText[509] = "Bandwidth Limit Exceeded";
function ajaxDone(responseText,target){
_getElementById(target).innerHTML = responseText;
}
function ajaxLoading(){
_getElementById("loader").innerHTML = "loading";
}
function ajaxLoaded(){
_getElementById("loader").innerHTML = "loaded";
}
var
funzioneDone = ajaxDone,
div = "test",
asincrono = false,
metodo = "get";
function test(link){
ajaxLoading();
var ajax = ajaxDo(link,funzioneDone,div,asincrono,metodo);
ajaxLoaded();
return ajax;
}
</script>
</head>
<body>
[url="http://localhost/post_get.php?a=b&c=d&f=g"]Test[/url]
<div id="loader">qui il div info</div>
<div id="test"></div>
</body>
</html>
Il contenuto della pagina post_get.php invece è questo:
Codice PHP:
<?php
sleep(5);
echo time()."POST: <pre>";
print_r($_POST);
echo "</pre>";
echo "GET: <pre>";
print_r($_GET);
echo "</pre>";
?>
Grazie per il tuo interessamento!