0

Hi there,

Let's say I have this HTML code below...

<html>
<head>
  <title>Page 1</title>
</head>
<body>
<form method="post" action="page2.jsp>
  <table>
    <tr>
      <td>John Doe</td>
      <td><input id="name1" name="name" type="submit" value="Edit" /></td>
    <tr>
    <tr>
      <td>Steve Smith</td>
      <td><input id="name2" name="name" type="submit" value="Edit" /></td>
    <tr>
    <tr>
      <td>Peter Pipper</td>
      <td><input id="name3" name="name" type="submit" value="Edit" /></td>
    <tr>
  </table>
</form>
</body>
</html>

How can I determine what submit button was pressed when I go to page2.jsp? For example, when I clicked on the first button, I want the name "John Doe" or input id "name1" at page2.jsp.

Thanks in advance :)

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by javaAddict
0

There are several ways. If you want to keep the code as it is then you can put on click events and change the type to button. You can have 3 different forms with their own submit buttons. Or you can put check-boxes or select-options instead of submit buttons.

One quick example is this:

<html>
<head>
  <title>Page 1</title>
</head>
<body>
<form method="post" name="formName" action="page2.jsp" >

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

  <table>
    <tr>
      <td>John Doe</td>
      <td><input id="name1" name="name1" type="button" value="Edit" 
onclick="document.formName.person.value='John Doe';" /></td>
    <tr>
    <tr>
      <td>Steve Smith</td>
      <td><input id="name2" name="name2" type="button" value="Edit" 
onclick="document.formName.person.value='Steve Smith';" /></td>
    <tr>
    <tr>
      <td>Peter Pipper</td>
      <td><input id="name3" name="name3" type="button" value="Edit" 
onclick="document.formName.person.value='Peter Pipper';" /></td>
    <tr>
  </table>
</form>
</body>
</html>

Search for tutorials at the w3schools about javascript and then about HTML DOM
By doing this: document.formName.person.value, You access the form with name="formName" then you access the input with name="person" and then you change its value.

What you do when you submit, you pass at the request the input field "person".

You can also have javascript that changes the action value of the form. If you don't want that hidden field you can have the onclick event to do this:
document.formName.action='page2.jsp?person=Peter Pipper'

At the page2.jsp use the request.getParameter("person") to take the value.

Don't forget to study this: http://www.w3schools.com/default.asp
You can find a complete reference of the HTML tags, how to use javascript and how to access the elements with DOM

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.