Copy link to clipboard
Copied
hello coldfusion community,
I refer to a previous post from 2016 and would like to continue on this example. It's about a product list with up to 10 items and to add, delete and edit a shopping cart. In my version everything works very well except if I want to change the field in the shopping cart with the amount (Quantity). I want the user to be able to change the value of quantity in the shopping cart. Somehow I do not get to the point. Does anyone have a suggestion or an idea for me? Thank you. Marcel M.
previous post from May 18, 2016: Extremely Simple Shopping Cart
products.cfm
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Shop - Productlist</title>
</head>
<body>
<h1>Welcome to the Shop</h1>
<p> </p>
<form action="ShoppingCart.cfm" method="post">
<p>Article-1, property 111, $45.00</p>
<input type="hidden" name="productID" value="1" />
<input type="hidden" name="productName" value="Article-1" />
<input type="hidden" name="productPrice" value="45.00" />
<input type="number" name="productAmount" />
<input type="submit" value="add to Cart" />
</form>
<form action="ShoppingCart.cfm" method="post">
<p>Article-2, property 222, $55.00</p>
<input type="hidden" name="productID" value="2" />
<input type="hidden" name="productName" value="Article-2" />
<input type="hidden" name="productPrice" value="55.00" />
<input type="number" name="productAmount" />
<input type="submit" value="add to Cart" />
</form>
</body>
</html>
ShoppingCart.cfm
<cfapplication name="App_08"
clientmanagement="no"
clientstorage="Cookie"
sessionmanagement="Yes"
setclientcookies="Yes"
sessiontimeout="#CreateTimeSpan(0,0,20,0)#">
<cfif NOT StructKeyExists(session,'ShoppingCart')>
<cfset Session.ShoppingCart = StructNew()>
<cfset Session.ShoppingCart.FirstName = "">
<cfset Session.ShoppingCart.LastName = "">
<cfset Session.ShoppingCart.Address = "">
<cfset Session.ShoppingCart.Products = ArrayNew(2)>
</cfif>
<cfif StructKeyExists(form,'productID')>
<cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)+1][1] = form.productID>
<cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][2] = form.productName>
<cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][3] = form.productPrice>
<cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][4] = form.productAmount>
</cfif>
<cfif StructKeyExists(url,'deleteID')>
<cfset variables.products = ArrayNew(2)>
<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">
<cfif url.deleteID NEQ i>
<cfset variables.products[arraylen(variables.products)+1][1] = Session.ShoppingCart.Products[1]>
<cfset variables.products[arraylen(variables.products)][2] = Session.ShoppingCart.Products[2]>
<cfset variables.products[arraylen(variables.products)][3] = Session.ShoppingCart.Products[3]>
<cfset variables.products[arraylen(variables.products)][4] = Session.ShoppingCart.Products[4]>
</cfif>
</cfloop>
<cfset Session.ShoppingCart.Products = variables.products>
</cfif>
<!--- my problems begins here --->
<cfif StructKeyExists(url,'changeID')> <!--- or with posts <cfif StructKeyExists(form,'changeID')> --->
<cfset variables.products = ArrayNew(2)>
<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">
<cfset currentFieldName2 = "productQuantity" & i > <!--- Dynamic-fieldname from form down --->
<cfif url.changeID EQ i> <!--- or with posts <cfif form.changeID EQ changeID --->
<cfset variables.products[arraylen(variables.products)+1][1] = Session.ShoppingCart.Products[1]>
<cfset variables.products[arraylen(variables.products)][2] = Session.ShoppingCart.Products[2]>
<cfset variables.products[arraylen(variables.products)][3] = Session.ShoppingCart.Products[3]>
<cfset variables.products[arraylen(variables.products)][4] = ??? FIELDNAME ??? >
</cfif>
</cfloop>
<cfset Session.ShoppingCart.Products = variables.products>
</cfif>
<!--- to here ... --->
<cfoutput>
<form name="ShoppingCart" action="ShoppingCart.cfm" method="post">
<h1>Your ShoppingCart</h1>
<p>You have #arrayLen(Session.ShoppingCart.Products)# article in your cart.<br /> (Session_Nr: #Session.SessionID#)<br /></p>
<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">
<cfset currentFieldName = "productQuantity" & i > <!--- Dynamic-fieldname to change the productQuantity --->
<table>
<tr>
<td>#Session.ShoppingCart.Products[2]#</td>
<td>#Session.ShoppingCart.Products[3]#</td>
<cfif structKeyExists(url, 'changeID')> <!--- or with posts </cfif><cfif isDefined("Form.submit")> --->
<td>New Quantity:<input type="text" name="#currentFieldName#"value="#variables.products[arraylen(variables.products)][4]#"></td>
<cfelse>
<td><input type="text" name="#currentFieldName#" value="#Session.ShoppingCart.Products[4]#">
<input type="hidden" name="changeID#i#" value="#Session.ShoppingCart.Products[1]#" />
</td>
</cfif>
<td><a href="ShoppingCart.cfm?deleteID=#i#">delete</a></td>
<td><a href="ShoppingCart.cfm?changeID=#i#">change</a></td>
<!--- or better with post? <td><input type="submit" name="submit" value="change"></td> ???--->
</tr>
</table>
</cfloop>
</form>
</cfoutput>
<p><a href="products.cfm">go back to the Shop / Product-list ...</a></p>
<p></p>
<hr>
<p></p>
<h1>Demo-Adress</h1>
<form action="adress.cfm" method="POST">
<div class="form-row">
<label>
<span>FirstName: </span>
<input type="text" size="20" name="FirstName">
</label>
</div>
<div class="form-row">
<label>
<span>LastName: </span>
<input type="text" size="20" name="LastName">
</label>
</div>
<div class="form-row">
<label>
<span>Address</span>
<input type="text" size="20" name="Address">
</label>
</div>
<input type="submit" class="submit" value="Yes shopping me">
</form>
Have something to add?