tags:

views:

24

answers:

1

Hello all im making a simple shopping cart in jsp. In my view cart file im running a loop to display the various items currently in cart with option of update and remove from cart buttons. When any of this buttons are clicked I pass the id and price as hidden input elements to another file to process it. This works for when there is only one record. But if there is suppose 2 or more items then it will pass the hidden elements 2 or more times..I have the code below and would appreciate your help. Thanks.

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.*, toyShop.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"&gt;
<%
    ArrayList<OrderLineData> orderedToys = (ArrayList<OrderLineData>)session.getAttribute("orderedToys");
    ToyManager toyM = new ToyManagerImp();
    ToyData tData = null;
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Shopping Cart</title>
    </head>
    <body>
        <h2>Shopping Cart</h2>


        <table border="1" cellspacing="6" cellpadding="6">
            <tr>
                <th>Toy Name</th>
                <th>Unit Price</th>
                <th>Quantity</th>
                <th>Cost</th>
                <th>Action</th>
            </tr>
            <%
                if(orderedToys != null){
                    for(int i =0; i < orderedToys.size(); i++){
                        tData = toyM.selectToy(MySQLConn.getInstance(), orderedToys.get(i).getToyID());
            %>
            <form name="frmCart" action="cart.jsp" method="get">
            <input type="hidden" name="txtToyID" value="<%=tData.getToyID()%>" />
        <input type="hidden" name="txtToyPrice" value="<%=tData.getPrice()%>" />
                <tr>
                    <td><%=tData.getToyName()%></td>
                    <td><%=tData.getPrice()%></td>
                    <td>
                        <input type="text" name="txtQuantity" size="3" value="<%=orderedToys.get(i).getOrderQuantity()%>" />                        
                    </td>
                    <td><%=orderedToys.get(i).getOrderCost()%></td>
                    <td>
                        <input type="submit" name="action" value="Update Cart" onclick=""/> | 
                        <input type="submit" name="action" value="Remove from Cart" />
                    </td>
                </tr>

            <%
                }
                }
            %>
              </form>
        </table>


        <a href="toy_search.jsp">Back to Search</a>
        || <a href="session_destroy.jsp">Remove All</a>
    </body>
</html>
A: 

Open the JSP page in your webbrowser. Rightclick and choose View Source to check the generated HTML code. Verify it on correctness (it is not correct), trackback the cause of the problem in JSP and fix it accordingly. If in vain, call for help at http://validator.w3.org.

BalusC
Yea i did, I was doing wrong as I have multiple form tags but only one </form> so the browser was confused which form to process. Well it is fixed now as stated above in the comment. Thanks anyways..
Deepesh
I hope you've at least learnt how to debug the problem.
BalusC