AJAX Javascript & PHP

Per trasmettere valori da script javascript e server esiste AJAX (Asynchronous JavaScript and XML).

L’output dello script deve essere in formato standard XML.

JSON, acronimo di JavaScript Object Notation, รจ un formato adatto all’interscambio di dati fra applicazioni client/server.

Sia Javascript che PHP supportano la codifica e decodifica dei dati in/da JSON.

JAVASCRIP

JSON.stringify(array)

PHP

json_decode($json_string)

JAVASCRIPT
Esempio di funzione per fare una richiesta AJAX

function richiesta_AJAX(pagina,parametri,ritorno){
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {
			// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
				if (typeof ritorno === "function") {
					ritorno(this.responseText);
				}
			}
		}
		xmlhttp.open("POST",pagina,true);
		//Send the proper header information along with the request
		xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlhttp.send(parametri);
	}

Leave a Reply