2

i have created three jsp pages main, view, store. In main.jsp i have a form , and i want to send data to view or store according to button i pressed.

i tried like this :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World!</h1>
    <form   method="post" >
        name<input type="text" name="name">
        age<input type="text" name="age">
        <a href="go_store.jsp" > <input type="button" value="Save" name="Save" 
 />
       <a href="go_view.jsp" >  <input type="button" value="view" name="view" />

    </form>
     </body>
 </html>
Srinivas Thanneeru
  • 161
  • 1
  • 2
  • 12

4 Answers4

3

@Srinivas, I hope the following will be usefull.

<form method="post" action="" id="form1">
 <input type="text" name="text1"/>
  <input type="button" value="Save" onclick="changeAction(this)"/>
  <input type="button" value="View" onclick="changeAction(this)"/>
</form>
<script>
  function changeAction(a)
   {
      var formEle=document.getElementById('#form1');
      if(a.value=="Save")
          formEle.setAttribute("action","go_save.jsp");
      else
          formEle.setAttribute("action","go_view.jsp");
   }
</script>

If you have any queries feel free to ask..

Madhu
  • 2,416
  • 3
  • 15
  • 33
1

You need not two different form for doing two different actions. What you need to do is find which button is clicked. Keep one type of input as submit and another as button. Then based on id you can get the click event. You can submit the form by script as well. Inside of the script do the stuff.

    $("#view").click(function() {
          // do your stuff what you need.
          $('form').submit();
    });

All you need is jquery plugin. Hope this helps..

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • i am very beginner in jsp, so can u please give some clear example of what your saying.. @vinoth krishnan – Srinivas Thanneeru Oct 02 '13 at 17:54
  • @SrinivasThanneeru In this forum what we can do is give/share some ideas with newbies. Have you visited these links in stackoverflow, have lots of implementations [Link1](http://stackoverflow.com/questions/10691653/multiple-submit-buttons-php-different-actions), [Link2](http://stackoverflow.com/questions/942772/html-form-with-two-submit-buttons-and-two-target-attributes), [Link3](http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form). Those ideas are implemented in php. You check with your code by implementing those answers. Hope you can do it. – Vinoth Krishnan Oct 03 '13 at 07:55
0

Hi I think you should use different forms for different button, normally a form should contain a single submit button. So use individual form for save and view.

Madhu
  • 2,416
  • 3
  • 15
  • 33
0

@Srinivas According to the your code without action attribute there is no possibility to send the data to the next page. You have to give the action attribute in the form element and need to change it dynamically according to the button clicked.

Madhu
  • 2,416
  • 3
  • 15
  • 33