Hej Alle
I lighed med andre har jeg problemer med indkøbskurven fra
www.asp24h.com (Kapitel 24)
Min xml og PrintBag(BagItems) fungerer, men når jeg retter antallet af
produkter og klikker på "Udregn igen" for at køre "Sub
RecalcBag(BagItems)", sættes antallet tilbage til 1.
Det samme sker når jeg klikker på "Gå til kassen"
Sub RecalcBag(BagItems) er plaseret nederst i koden.
MIN KODE ER:
<% Response.Buffer = True %>
<html>
<head>
<title>Implementing the Shopping Bag</title>
</head>
<body bgcolor="#ffffff">
<%
' get or create the bag
Set BagItems = LoadXMLBag()
' add or modify products
If Request.ServerVariables("CONTENT_LENGTH") > 0 Then
Select Case Trim(Request.Form("DO"))
Case "Flere indkøb"
Response.Redirect "Test1.asp"
Response.End
Case "Udregn igen"
RecalcBag BagItems
Case "Gå til kassen"
' Udregn igen and then redirect to checkout
End Select
Else
' products are added using the query string
Msg = Trim(Request.QueryString("Test"))
AddtlProduct Msg, BagItems
End If
Session("BagItems") = BagItems.XML
If BagItems.hasChildNodes Then
PrintBag BagItems
Else
Response.Write "Du har ikke varer i din indkøbskurv!"
End If
%>
</body>
</html>
<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
Function LoadXMLBag()
Set XMLDoc = Server.CreateObject("Microsoft.XMLDOM")
If Not IsEmpty(Session("BagItems")) Then
XMLDoc.loadXML CStr(Session("BagItems"))
Set BagItems = XMLDoc.documentElement
Else
Set BagItems = XMLDoc.createElement("BagItems")
Set XMLDoc.documentElement = BagItems
End If
Set LoadXMLBag = BagItems
End Function
Sub AddtlProduct(Msg, BagItems)
bValid = True
If "" = Msg Or Not IsNumeric(Msg) Then
bValid = False
Else
set cnn = server.CreateObject("Adodb.connection")
Cnn.ConnectionString = Application("Connectionstring")
Cnn.Open
set RS = Server.CreateObject("ADODB.Recordset")
set RS.ActiveConnection = cnn
RS.Open "SELECT tblProdukter.* FROM tblProdukter WHERE ProduktId=" &
Msg
If RS.EOF And RS.BOF Then
bValid = False
Else
avarProduct = RS.GetRows()
End If
CNN.Close
Set RS = Nothing
Set CNN = Nothing
End If
' if the product was ok, add it to the bag
If bValid Then
AddProductToBag avarProduct, BagItems
End If
End Sub
Sub AddProductToBag(avarProduct, BagItems)
Set XMLDoc = BagItems.ownerDocument
Set nodeFind = BagItems.selectNodes("product[@produktID=""" & Msg &
"""]")
If Not nodeFind.length > 0 Then
Set product = XMLDoc.createElement("product")
Set attr = XMLDoc.createAttribute("produktID")
attr.Text = avarProduct(0,0)
product.attributes.setNamedItem(attr)
Set attr = XMLDoc.createAttribute("Navn")
attr.Text = Server.HtmlEncode(avarProduct(1,0))
product.attributes.setNamedItem(attr)
Set attr = XMLDoc.createAttribute("Beskrivelse")
attr.Text = Server.HtmlEncode(avarProduct(2,0))
product.attributes.setNamedItem(attr)
Set attr = XMLDoc.createAttribute("Pris")
attr.Text = avarProduct(3,0)
product.attributes.setNamedItem(attr)
product.text = "1"
BagItems.appendChild(product)
End If
End Sub
Sub Out(strOut)
Response.Write strOut
End Sub
Sub PrintBag(BagItems)
Out "<FORM METHOD=""POST"" ACTION="""
Out Request.ServerVariables("SCRIPT_NAME") & """>"
Out "<TABLE><TR><TH>Antal</TH><TH COLSPAN=2>"
Out "Produkt</TH><TH>Enhedspris</TH><TH>Pris</TH>"
Out "</TR>" & vbCrLf
For Each item In BagItems.childNodes
Out "<TR><TD><input type=""TEXT"" name=""Qty"
Out "" & item.getAttribute("produktID")
Out """ VALUE=""" & item.text & """ SIZE=""3"">"
Out "<TD>""" & item.getAttribute("Navn") & "</TD>"
Out "<TD>""" & item.getAttribute("Beskrivelse") & "</TD>"
nUnitPrice = "" & item.getAttribute("Pris")
nSubTotal = item.text * nUnitPrice
nTotal = nTotal + nSubTotal
Out "<TD>" & FormatCurrency(nUnitPrice) & "</TD>"
Out "<TD><B>" & FormatCurrency(nSubTotal) & "</B></TD>"
Out "</TR>" & vbCRLF
Next
Out "<TR><TD COLSPAN=5><HR></TD></TR>"
Out "<TR><TD COLSPAN=3></TD><TD><B>Total:</B></TD>"
Out "<TD><B>" & FormatCurrency(nTotal) & "</B></TD></TR>" & vbCrLf
Out "<TR><TD COLSPAN=5 ALIGN=""RIGHT"">"
Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Flere indkøb""> "
Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Udregn igen""> "
Out "<INPUT TYPE=SUBMIT NAME=""DO"" VALUE=""Gå til kassen"">"
Out "</TD></TR></TABLE></FORM>"
End Sub
Sub RecalcBag(BagItems)
For Each item in BagItems.childNodes
nQty = Trim(Request.Form("Qty""" & item.getAttribute("produktID")))
If "" <> nQty And IsNumeric(nQty) Then
If nQty < 1 Then
BagItems.removeChild item
Else
item.text = nQty
End If
End If
Next
End Sub
</SCRIPT>
Hilsen Jørgen