Scusate x il titolo un po insignificante ma nn sapevo cosa scrivere, navigando in internet ho trovato e usato uno script x provare il funzionamento di ajax. Si tratta di poche righe di codice che riporto qui sotto:
prova.html
codice:
<!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 language="javascript" type="text/javascript">
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e2){
xmlHttp = false;
}
}
@end @*/
if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){
xmlHttp = new XMLHttpRequest();
}
function callServer(){
var city = document.getElementById("city").value;
var state = document.getElementById("state").value;
if((city==null) || (city=="")) return;
if((state==null) || (state=="")) return;
var url = "http://localhost/boh/prova.php?city=" + escape(city) + "&state=" + escape(state);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage(){
if(xmlHttp.readyState == 4){
var response = xmlHttp.responseText;
document.getElementById("zipcode").value = response;
}
}
</script>
</head>
<body>
<input type="text" id="city" />
<input type="text" id="state" />
<input type="text" id="zipcode" />
<input type="button" value="Invia" onclick="callServer();" />
</body>
</html>
e la pagina di risposta prova.php
codice:
<?php
$citta = (string)trim($_GET['city']);
$stato = (string)trim($_GET['state']);
if(($citta=="pavia") && ($stato=="italia")){
echo "27100";
}else{
echo "Non so";
}
?>
Ora a parte la stupidità del codice scritto (è solo una prova x vedere se il webserver risponde ed ha risposto) le mie domande sono le seguenti (essendo proprio agli inizi):
1. ma questo semplice script mi basta x sfruttare le potenzialità di ajax nel senso il tutto si limita a passare una richiesta con quella semplice funzione e restituire un echo dal php (quindi cmq fa tutto php)??
2. come posso fare la stessa cosa utilizzando il metodo POST??
Scusate le domande stupide (soprattutto la prima) ma mi piacerebbe capire che potenzialità ulteriori possono esserci in questa tecnologia, visto che in italiano ho trovato poco sul web..
ciao e grazie.