tags:

views:

533

answers:

1

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.

A: 

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?

Cory Larson
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
cc
Oh, I see. Because you have a hidden input element for every row, it's sending all of the values to the server.
Cory Larson
Nope, even If a remove the hidden type, it's still sending all the values.
cc
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