Files
unisa_tsw_2014_2015/ajax2_json.html
2014-12-07 19:33:49 +01:00

63 lines
1.3 KiB
HTML

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p> Ciao, Simone è gay?</p>
<p id="answer"></p>
<form id="form">
<input type ="text" name="nome" placeholder="Inserisci il nome" value="" />
<input type ="text" name="cognome" placeholder="Inserisci il cognome" value=""/>
<input type ="number" name="eta" min="10" max="120" placeholder="Inserisci eta" value="" />
</form>
<br>
<span id ="data"></span>
<span id ="debug"></span>
<script>
req= new XMLHttpRequest();
req.addEventListener("load",handlerload,false);
i = document.getElementsByTagName("input");
for (k=0;k< i.length;k++)
i[k].addEventListener("input",handler,false);
function handler(e) {
dati={};
form = (document.getElementById("form"));
fc = form.children;
for (i = 0; i < fc.length; i++){
if ((fc[i].tagName == "INPUT") && (fc[i].hasAttribute("name") ) ) {
dati[fc[i].getAttribute("name")] = fc[i].value;
}
}
req.open("POST","ajax2_json.php",true);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(dati));
}
function handlerload(e){
document.getElementById("data").innerHTML = req.responseText;
}
</script>
</body>
</html>