简体   繁体   中英

how to add these the items in a cart “jsp”

I'm using the following code to display my data from a database but I'm unable to add data to the cart on event like when I click the "add to cart" button

query = "select * from books";
rs = st.executeQuery(query);
out.println("<h4><marquee> WELCOME TO BOOK SECTION </marquee></h4>");
out.println("<table border=1>");
count=1;
while (rs.next()) {
    i++;
    itemName = rs.getString("BOOK_NAME");
    avail = rs.getString("BOOK_AVAIL");
    cost = rs.getFloat("BOOK_sell_price");
    if(count==1){
        out.print("<tr>");
    }   
    out.println("<td>" + itemName  +  " "+  avail + "  " + cost + "<br>");
    out.println("<input type=button value='add to cart' onclick=addcart();>");
    out.print("</td>");
    count+=1;
    if(count>3){
        out.print("</tr>");
        count=1;
    }

Can anyone give an appropriate method to add these items in the backend so that when I go to cart.jsp I'll be able to find the item on cart for which I have clicked "add to cart"?

I don't know how to use session without manipulating t above code and if any other method available please let me know.

Thank you well in advance.

Just put each add button in its own <form> wherein you define the product ID as a hidden input field. You of course have a column representing the product ID in the DB table, do you?

<form action="add" method="post">
    <input type="hidden" name="id" value="${id}" />
    <input type="submit" value="Add to cart" />
</form>

This way the id is available as request parameter. No need for ugly Javascript hacks which would make your website unusable on websites with JS disabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM