File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes getting id, not value from form element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "getting id, not value from form element" Watch "getting id, not value from form element" New topic

getting id, not value from form element

Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
I have a page that pulls from db, and corresponding to each record, I have two buttons (one for edit, one for report).
On the edit page, I need to know which record the button pressed corresponds to, so I can pull the record from the db. Problem is, all buttons have the same "value". If I use request.getParameter, I'm only going
to get the value, when what I need is the "id". I don't know how this is done. Any advice will be much appreciated!

input name="Submit" type="submit" class="buttonNeutral" **id="<%=goID[i]%>" value="Edit"

I'm wanting to know how to get the "id" value on another jsp, not the "value" value
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

Create a hidden text field in your from with the value from your array. Since you will know the name you assigned to the hidden field, you will be able to get the value easily.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
You could just use the name attribute of the button - it does not have to be Submit.
The existance of a parameter with that name - as opposed to the names of the other buttons - will be sufficient.
Of course, hidden values are more flexible because you can have lots of them.
Sutham Rojanusorn

Joined: Jul 10, 2004
Posts: 4
just adding some hidden field to your form

<input type="hidden" name="actionType" value="<%=goID[i]%>">
<input name="Submit" type="submit" class="buttonNeutral" **id="<%=goID[i]%>" value="Edit">
M. Gagnon
Ranch Hand

Joined: Feb 18, 2004
Posts: 68
I used a hyperlink in a similar situation instead of buttons:

<a href="recordDetailPage.jsp?recordID=<%=idsArr[i]%>">Detail Page</a>

Of course, if there are other form fields you want to submit, you'll have to append the name/value pairs to the query string above.

On the other hand, if you're set on using buttons . . .

If you use a separate hidden field for each record, they will all be submitted with the form unless you have a separate form for each button/hidden field pair (which seems like a tedious solution). If you use only one hidden field, you could set the id value with some client-side javascript. Define a javascript method to set the value parameter of the hidden field that's called in the onclick event of all the buttons.
I agree. Here's the link:
subject: getting id, not value from form element
It's not a secret anymore!