Link to home
Create AccountLog in
Avatar of Andrew Angell
Andrew AngellFlag for United States of America

asked on

How to loop through a form's checked check box items to add each one to my shopping cart...???

I use a 3rd party shopping cart that's very easy to configure for adding single items to a cart. It's not very well setup, though, for creating 1 form of products with check boxes and adding every checked item to the cart with a single form submission.

I'm trying to do this manually by creating the form myself and calling the add to cart function the shopping cart software uses for each checked item.  Does this sound like how I should do it?

If so, how do I do that? lol.  When the form is submitted how do I loop through each of the check boxes that are checked?  The products with check boxes are listed dynamically on the page.  The check box names are all ATCCheck and the checked values of each is its productID.  

The form also includes a hidden form field with another product that needs to be included in the loop and added at the same time as everything else.

Any information would be greatly appreciated.  Thanks!
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Andrew Angell

ASKER

Well, shoot.  I may have jumped the gun.  Would you mind taking a look and seeing what I've done wrong here?  I have the following:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="WA_eCart/WA_eCart_Definition_VB.asp" -->
<!--#include file="WA_eCart/kennysCart_VB.asp" -->
<!--#include file="Connections/connKennysMDIData.asp" -->
<%
checkedProducts = Request.Form("ATCCheck")
checkedProductsSplit = Split(checkedProducts,",")
for i = LBound(checkedProductsSplit) to UBound(checkedProductsSplit)
%>
<%
Dim rsAddedItems
Dim rsAddedItems_cmd
Dim rsAddedItems_numRows

Set rsAddedItems_cmd = Server.CreateObject ("ADODB.Command")
rsAddedItems_cmd.ActiveConnection = MM_connKennysMDIData_STRING
rsAddedItems_cmd.CommandText = "SELECT kennysInventoryID, productDescription1, productID, productName, unitPrice, weight FROM products  WHERE productID = "&i&""
rsAddedItems_cmd.Prepared = true

Set rsAddedItems = rsAddedItems_cmd.Execute
rsAddedItems_numRows = 0
%>
<%
'WA eCart Get Info From RS
if ("" = "")     then
  Redirect_redirStr = "shoppingCart.asp"
  while (NOT rsAddedItems.EOF)
    kennysCart_itemID = cDbl("" & cStr((rsAddedItems.Fields.Item("productID").Value)) & "")
    kennysCart_itemName = "" & cStr((rsAddedItems.Fields.Item("productName").Value)) & ""
    kennysCart_itemDescription = "" & cStr((rsAddedItems.Fields.Item("productDescription1").Value)) & ""
    kennysCart_itemWeight = cDbl("" & cStr((rsAddedItems.Fields.Item("weight").Value)) & "")
    kennysCart_itemQuantity = cDbl("1")
    kennysCart_itemPrice = cDbl("" & cStr((rsAddedItems.Fields.Item("unitPrice").Value)) & "")
    set kennysCart = kennysCart_AddToCart(1, "", kennysCart_itemID, kennysCart_itemName, kennysCart_itemDescription, kennysCart_itemWeight, kennysCart_itemQuantity, kennysCart_itemPrice)
    rsAddedItems.MoveNext()
  wend
  if (rsAddedItems.CursorType > 0)     then
    if (NOT rsAddedItems.BOF)     then
      rsAddedItems.MoveFirst()
    end if
  else
    rsAddedItems.Requery()
  end if
  if (Redirect_redirStr <> "")     then
    kennysCart_redirStr = Redirect_redirStr
  end if
end if
%>
<%
' WA eCart Redirect
if (kennysCart_redirStr <> "")     then
  Response.Redirect(kennysCart_redirStr)
end if
%>

<%
Next
%>

<%
rsAddedItems.Close()
Set rsAddedItems = Nothing
%>


And it's adding a whole bunch of products to the shopping cart instead of just the few that are in the form.  Not sure how it's grabbing them...they're pretty random.
Change
FROM products  WHERE productID = "&i&""
to
FROM products  WHERE productID = "& checkedProductsSplit (i) &"" 
Ok, that fixed that.  Thank you.  I am having another issue, though, explained for extra points in this thread: https://www.experts-exchange.com/questions/21984932/Problems-with-a-form-post-and-looping-through-the-checked-items.html.

Basically I have 2 tables within the form that's getting submitting that dynamically generate check boxes for items.  All check box names are the same and reside within the same form, but only the second table's check boxes are working.