0

I'm new in jquery and ajax, i just want to open all my navigation links in one page. I also divided my page in three division. 1st page is in html i.e. welcome.html and rest all are in jsp. simply program takes two numbers, performs arithmetic operations on server (i wrote the servlet for each operation) side and send result back to jsp. At first it opens on same page but when i click on calculate button it goes on next page. if anybody have this solution using ajax or jquery plz let me know .

enter image description hereenter image description hereenter image description hereenter image description here

mahi
  • 39
  • 3

2 Answers2

0

It's probably because the calculate button submits the form. Try calculating the value with JQuery/AJAX.

0

Keep it like this(Using javascript, though you can use jquery also):

Say the id of first number relaed input field is val1 and that of second value is val2, Keep this javascript function code in your jsp at bottom:

<script>
function calculate(operation){
var value1, value2, result;
value1=document.getElementById("val1").value;
value2=document.getElementById("val2").value;
if(operation=='add')
result=value1+value2;
else if(operation=='subtract')
result=value1-value2;
else if(operation=='multiply')
result=value1*value2;
else if(operation=='divide')
result=value1/value2;
else
result="invalid choice!";
return result;
}
</script>

Each time on calculate button(like add, subtract, multiply, divide) keep:
for add button-> onclick=calculate('add')
for subtract button-> onclick=calculate('subtract')
for multiply button-> onclick=calculate('multiply')
for divide button-> onclick=calculate('divide')

inside calculate button onclick event.

Note: If you really want to use servlets, use ajax call for that with your existing code, you can see an example here and here

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28