- Codice: Seleziona tutto
<!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=utf-8" />
<title>Documento senza titolo</title>
<script type="text/javascript">
function valida()
{
var n1=document.getElementById("num1");
var n2=document.getElementById("num2");
var url="validate?";
url+="num1="+escape(n1);
url+="&";
url+="num2="+escape(n2);
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(xmlhttp.readyState==4)
{document.getElementById('ris').innerHTML=xmlhttp.responseText}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
</script>
</head>
<body>
<h1 style="text-align:center"> Form Ajax </h1>
<form id="form1">
Numero 1:
<label>
<input type="text" name="num1" id="num1" />
</label>
<p>
<label>
Numero 2:
<input type="text" name="num2" id="num2" />
</label>
</p>
<p>
Somma
<label>
<input type="radio" name="radio" id="somma" value="somma" onkeyup="valida()" />
Differenza
<input type="radio" name="radio" id="differenza" value="differenza" onkeyup="valida()" />
</label>
</p>
<p>
<label>
<input type="reset" name="cancella" id="cancella" value="cancella" />
</label>
</p>
</form>
<div id="ris"></div>
</body>
</html>
---------------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ValidateServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String n1 = req.getParameter("num1");
String n2 = req.getParameter("num2");
String op=req.getParameter("radio");
if(op=="somma")
{
res.setContentType("text/html");
res.getWriter().write(n1+n2);
}
}
}
Spero che mi possiate aiutare perchè ho urgente bisogno di questa cosa in quanto sto facendo un sito di matematica per dei bambini. grazie.
RSS