<html><head>
<script language="JavaScript">
function update()
{
confirm("Are you sure?(Yes/No)");
document.forms[0].submit();
}
</script></head>
<body>
<form name="form1" method="post" action="controller/servlet">
<input type="submit" name="event" value="Add">
<input type="button" name="event" value="Update" onClick="update();">
</form>
</body>
</html>

here, on the click of both buttons "Add" and "Update",the control should go to the servlet.While clicking Update, the script runs and then the control transfers to servlet. But the name of the button Update is not read. What should be done so that the button value will be read in the servlet and the corresponding action takes place.
please help..

First of all we need to see the code at the servlet.
Second, you can have a hidden parameter in the jsp and the user clicks update to change its value:

<html><head>
<script language="JavaScript">
function update()
{
var r=confirm("Are you sure?");
if (r==true)
  {
document.form1.action.value='update';
document.forms[0].submit();
  }
else
  {
document.form1.action.value='';
  }
}
</script></head>
<body>
<form name="form1" method="post" action="controller/servlet">
...
...

<input type="hidden" name="action" value="" />

</form>
</body>
</html>

Also check these 2 links:

http://www.w3schools.com/default.asp
http://www.w3schools.com/js/default.asp

code at the servlet is jus checking the value of the button and redirect to another page.

In servlet:

String event=request.getParameter("event");
if(event.equals("Update"))
{
response.sendRedirect("result.jsp");
}

Here when i print the value of the string event. Nothing is printed.

Have you tried my suggestion? I believe that you can't get the value from a "button", but I might be wrong.

what u said works!!!!! Is there any other more efficient of achieving this????

Have you tried using 2 different forms?

<form name="form1" method="post" action="controller/servlet">

  <input type="submit" name="event" value="Add">
</form>

<form name="form2" method="post" action="controller/servlet"
 onsubmit="return confirm("Are you sure?");" >

<input type="submit" name="event" value="Update" >
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.