2

I'm trying to add to every row of a HTML table a "delete" and a "modify" button. If I use this method, the value of the "id" is alawys the value of id from the first row, even if I pressed the button from the rows 2-n.

<%  if (listx.size() > 0)
    {
        int j = 0;
        for (int i = 0; i < listx.size(); i++)
        {
            Contact x= listx.get(i);
%>
<form action="servl" method="get">
    <tr>
        <td><%=++j%>            </td>
        <td><%=x.getName()%>            </td>
        <td><%=x.getCar()%>            </td>
        <td><%=x.getZip()%>            </td>

        <td>
            <input type="hidden" name="ID" value="<%=x.getId()%>">
            <input type="submit" name="action" value="modify">
            <input type="submit" name="action" value="delete"</td>
    </tr>
<%
    } %>

P.S. 1. This could be a solution: http://www.daniweb.com/forums/post983361.html#post983361 (post 10) but I need that my inputs to be in this format:

<input type="submit" name="action" value="NameOfTheAction">

Later Edit: Don't say anything about the scriplets. I will use JSTL later. :)

Problem solved: I found out the problem. I didn't closed the form tag for every row. I closed the tag only at the finish of the table (creating a form for the whole table), so every time I pressed a row button, every "id" was send.

cc.
  • 5,533
  • 14
  • 37
  • 46

1 Answers1

0

Your Contact is assigned to x but you're using contact in your hidden "ID". Also, why the need for the extra j variable... couldn't you just use (i + 1)?

EDIT:

I see you sneakily updated your code to correct the variable problem. Are you still having issues? Some obvious questions... Does each of your contacts have an ID, and is getId() returning the correct value if you loop through them outside of the JSP?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • 1. Yes, you're rite, I changed the name of the variabiles a little, because they're in a another language. 2. Probably, but this not my problem now. :) – cc. Oct 07 '09 at 11:51
  • Yes, I sneakly updated the code. :)) 2. Yes, every contact has his own id. I used "Page source" from Firefox and every row has his own unique id. If I use the "get" method (HTML),the URL is something like this: ID=1&ID=2&action=modify&ID=3&ID=4&ID=5 – cc. Oct 07 '09 at 12:00
  • Oh, I see. Because you have a hidden input element for every row, it's sending all of the values to the server. – Cᴏʀʏ Oct 07 '09 at 12:09
  • I found out the problem. I didn't closed the form tag for every row. I close the tag only at the finish of the table, so every time I pressed a row button, every "id" was send. – cc. Oct 07 '09 at 12:47