dante dante - 4 years ago 187
SQL Question

Spring MVC - delete row in SQL with JSP table

I got another issue with place "delete" buttons in my cart (SQL table)
I want in order to when I get cursor on delete button in for example row with ID 36 then link in that button will be suitable for row's ID, 36.

This is my code:

<form action="/OnlineShop/cart/selectedItem=${cart.getID()}" method="post">
<div style="padding-right: 40px">
<table border="1">
<tr>
<td>ID</td>
<td>Product</td>
<td>Name</td>
<td>Company</td>
<td>Type</td>
<td>Price</td>
<td>Action</td>
</tr>
<c:forEach var="cart" items="${carts}">
<tr>
<td>${cart.getID()}</td>
<td><img src="${cart.getProduct()}" /></td>
<td>${cart.getName()}</td>
<td>${cart.getCompany()}</td>
<td>${cart.getType()}</td>
<td>${cart.getPrice()}</td>
<td><input type="submit" value="Delete"/></td>
</tr>
</c:forEach>
</table>
</div>
</form>


I'm not surprised that it doesn't work just like I wanted (IDs doesn't display when I get my cursor on delete because form action doesn't know what is this String?) and I tried something like that:

<c:forEach var="cart" items="${carts}">
<form action="/OnlineShop/cart/selectedItem=${cart.getID()}" method="post">
<div style="padding-right: 40px">
<table border="1">
<tr>
<td>ID</td>
<td>Product</td>
<td>Name</td>
<td>Company</td>
<td>Type</td>
<td>Price</td>
<td>Action</td>
</tr>
<tr>
<td>${cart.getID()}</td>
<td><img src="${cart.getProduct()}" /></td>
<td>${cart.getName()}</td>
<td>${cart.getCompany()}</td>
<td>${cart.getType()}</td>
<td>${cart.getPrice()}</td>
<td><input type="submit" value="Delete"/></td>
</tr>
</table>
</div>
</form>
</c:forEach>


It worked good with exception that every row was seperated with rest rows. It's like one table (with all marked columns) is responsible for one row from my SQL database. How can I fix this?

Answer Source

You can do it in JS, but it can works for you:

<div style="padding-right: 40px">
      <table border="1">
          <tr>
              <td>ID</td>
              <td>Product</td>
              <td>Name</td>
              <td>Company</td>
              <td>Type</td>
              <td>Price</td>
              <td>Action</td>
          </tr>
          <c:forEach var="cart" items="${carts}">
              <tr>
                  <td>${cart.getID()}</td>
                  <td><img src="${cart.getProduct()}" /></td>
                  <td>${cart.getName()}</td>
                  <td>${cart.getCompany()}</td>
                  <td>${cart.getType()}</td>
                  <td>${cart.getPrice()}</td>
                  <td><form action="/OnlineShop/cart/selectedItem=${cart.getID()}"  method="post"><input type="submit" value="Delete"/></form></td>
              </tr>
              </c:forEach>
      </table>        
</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download