ajax e dom
This commit is contained in:
63
ajax2_json.html
Normal file
63
ajax2_json.html
Normal file
@@ -0,0 +1,63 @@
|
||||
<!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>
|
||||
Reference in New Issue
Block a user