Adobe COLDFUSION 9 Manual page 86

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Code
<cfscript>
CartItems=4;
Cart = ArrayNew(1);
for ( i=1; i LE cartItems; i=i+1)
{
Cart[i]=StructNew();
Cart[i].ID=i;
Cart[i].Name="Product " & i;
Cart[i].SKU=i*100+(2*i*10)+(3*i);
Cart[i].Qty=3*i-2;
}
</cfscript>
<cfform name="ShoppingCart"
action="ShoppingCartAction.cfm" method="post">
<table>
<tr>
<td>Order?</td>
<td>Product</td>
<td>Code</td>
<td>Quantity</td>
</tr>
<cfloop index="i" from="1" to="#cartItems#">
<tr>
<cfset productName= "product_" & Cart[i].ID>
<cfset skuName= "sku_" & Cart[i].ID>
<cfset qtyname= "qty_" & Cart[i].ID>
<td><cfinput type="checkbox" name="itemID"
value="#Cart[i].ID#" checked>
</td>
<td><cfinput type="text" name="#productName#"
value="#Cart[i].Name#"
passThrough = "readonly = 'True'"></td>
<td><cfinput type="text" name="#skuName#"
value="#Cart[i].SKU#"
passThrough = "readonly = 'True'"></td>
<td><cfinput type="text" name="#qtyName#"
value="#Cart[i].Qty#">
</td>
</tr>
</cfloop>
</table>
<input type="submit" name="submit" value="submit">
</cfform>
Create the Action page
Create a file in your editor.
1
2
Enter the following text:
Description
Create a shopping cart as an array of structures, with each structure
containing the cart item ID, product name, SKU number, and quantity
ordered for one item in the cart. Populate the shopping cart by
looping CartItems times and setting the structure variables to
arbitrary values based on the loop counter. A real application would
set the Name, SKU, and Quantity values on other pages.
Start the form and its embedded table. When the user clicks the
submit button, post the form data to the ShoppingCartAction.cfm
page.
The table formats the form neatly. The first table row contains the
column headers. Each following row has the data for one cart item.
Loop through the shopping cart entries to generate the cart form
dynamically. For each loop, generate variables used for the form field
name attributes by appending the cart item ID (Cart[i].ID) to a field
type identifier, such as "sku_".
Use a single name, "itemID", for all check boxes. This way, the itemID
value posted to the action page is a list of all the check box field
values. The check box field value for each item is the cart item ID.
Each column in a row contains a field for a cart item structure entry.
The
passthrough
read only; note the use of single-quotation marks. (For more
information on the cfinput tag
Reference.) The check boxes are selected by default.
Create the Submit button and end the form.
Last updated 8/5/2010
attribute sets the product name and SKU fields to
passthrough
attribute, see the CFML
81

Advertisement

Table of Contents
loading

Table of Contents