2012-09-18 60 views
0

下面每次畫廊只增加1個產品我點擊添加到購物車,我希望它通過ID爲所選的特定項目從<foreach tag>

問題: 1 - 該servlet加1個產品加入購物車(第一個)和URL看起來像這樣: 本地.../DisplayCart的productId = 01 &的productId = 02 &行動= addtocart

<form action="/DisplayCart"> 
<c:forEach var="product" items="${products}"> 
        <tr> 
         <td>${product.name}</td> 
         <td>${product.description}</td> 
         <td>${product.price}</td> 
         <td></td> 
         <td> 
          <input type="hidden" name="productId" value="${product.id}"> 
          <input type="submit" name="action" value="addtocart"></td> 
        </tr> 
       </c:forEach> 

     <form action="<c:url value='/DisplayCart'/>"> 
     <table> 
      <c:forEach var="item" items="${cart.items}"> 
        <tr> 
         <td> 
     <input type="hidden" name="productId" value="${item.product.id}"> 
     <input type="text" size="2" name="quantity" value="${item.quantity}"> 
          <input type="submit" value="Update"> 
         </td> 
         <td>${item.product.name}</td> 
         <td>${item.product.description}</td> 
         <td>${item.product.price}</td> 
       <td><input type="submit" name="removeButton" value="Remove">  </td> 
        </tr> 
      </table> 
       </form> 
      </c:forEach> 
+0

沒有其實我是想用戶是能夠選擇任何這個循環基本上從數據庫中列出的項目..無論如何感謝 – TrackmeifYouCan

回答

0

,我發現它的很大一部分。

替換此

 <td><input type="submit" name="removeButton" value="Remove">  </td> 

與此

 <td><a href="<c:url value='/DisplayCart?productId=${product.id}'/>">add</a></td> 

和URL始終顯示爲/ DisplayCart?productId參數= XX

happyyy

+0

在同一時間,我會感謝有人解釋爲什麼它不是以前的方式工作?即使removeButton也只是刪除第一個。 – TrackmeifYouCan