如何将这些商品添加到购物车“jsp”中
我正在使用以下代码来显示数据库中的数据,但我无法在事件中将数据添加到购物车,例如当我单击“添加到购物车”按钮时
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;
}
任何人都可以提供适当的方法来将这些项目添加到后端,以便当我转到 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将每个添加按钮放入其自己的
这样,
id
就可以作为请求参数使用。无需丑陋的 Javascript 黑客攻击,这会使您的网站在禁用 JS 的网站上无法使用。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?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.