Vp Asp Shopping Cart ❲Plus | TIPS❳

Session("Cart") = cart Response.Redirect("view_cart.asp") %> At checkout, copy cart to database , then clear Session:

Session("Cart") = cart Response.Redirect("view_cart.asp") %> <% cart = Session("Cart") total = 0 %> <table border="1"> <tr><th>Product</th><th>Price</th><th>Qty</th><th>Subtotal</th><th></th></tr> <% For i = 0 To UBound(cart) subtotal = cart(i, 2) * cart(i, 3) total = total + subtotal %> <tr> <td><%=cart(i,1)%></td> <td><%=FormatCurrency(cart(i,2))%></td> <td> <form method="post" action="update_cart.asp"> <input type="hidden" name="idx" value="<%=i%>"> <input type="number" name="qty" value="<%=cart(i,3)%>" min="0" style="width:60px"> <input type="submit" value="Update"> </form> </td> <td><%=FormatCurrency(subtotal)%></td> <td><a href="remove_item.asp?idx=<%=i%>">Remove</a></td> </tr> <% Next %> <tr><td colspan="3">Total</td><td><%=FormatCurrency(total)%></td><td></td></tr> </table> <a href="checkout.asp">Checkout</a> 5. Update cart ( update_cart.asp ) <% Dim idx, new_qty, cart idx = CInt(Request("idx")) new_qty = CInt(Request("qty")) cart = Session("Cart") If idx >= 0 And idx <= UBound(cart) Then If new_qty > 0 Then cart(idx, 3) = new_qty Else ' remove item For i = idx To UBound(cart) - 1 cart(i) = cart(i + 1) Next ReDim Preserve cart(UBound(cart) - 1) End If End If vp asp shopping cart

Sub Session_OnStart Session("Cart") = Array() End Sub : Array(ProductID, ProductName, Price, Quantity) 3. Add to cart ( add_to_cart.asp ) <% Dim pid, pname, price, qty, cart, found, i pid = Request("id") pname = Request("name") price = CDbl(Request("price")) qty = CInt(Request("qty")) If qty < 1 Then qty = 1 Session("Cart") = cart Response

(store in Session("Cart") ):