开发者

delete row corresponding to button clicked in jsp

  userdetails.jsp

  <tr>

 <td>
 <%
  out.println(rs.getString(1));
  name=rs.getString(1);
  out.print("<input type='hidden' name='user' value='"+name+"'");
  %>

</td>


<td>
<%out.println(rs.getString(2));

    %>
</td>
    <td>
          <%out.println(rs.getString(3));

    %>
    </td>
    <td>
        <%out.println(rs.getString(4));

    %>
    </td>

    <td>
        <i开发者_开发技巧nput type="Submit" name="delete_user" value="Delete"/>
    </td>

    </tr>

when i click the delete button only first row is getting deleted and not the row corresponding to which button is clicked


You're putting multiple hidden input values and delete buttons altogether in the same form. When you use request.getParameter() to get the hidden input value, you will indeed only get the first one, regardless of the delete button pressed.

You need to group the hidden input and the delete button in their own forms.

<td>
    <form action="delete" method="post">
        <input type="submit" name="delete_user" value="Delete" />
        <input type="hidden" name="user" value="<%=rs.getString(1)%>" />
    </form>
</td>

This way the request will always have the one and the right user name as parameter.


Said that, using scriptlets in JSP is 90's way of writing JSPs and also mingling database logic in a JSP is not really a good practice. I'd suggest to get yourself through this answer.


You can add the user name to the button value upon clicking it:

<input type="Submit" name="delete_user" value="Delete" onclick="this.value += ' <% out.print(name); %>'; this.disabled = true; " />

Then in the server side code, parse the value: get the text after first space, which is the user name to delete and reference only row with that user name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜