In this example, we will create the list on one page and pass it to another using a URL variable. The second page will use this list to build a drop-down menu. -
Open your text editor and type the code in Listing 10.1 or you can open the List1.cfm file from the CompletedFiles\Examples\Step10 folder. Listing 10.1 List1.cfm <!--- File: List1.cfm Description: Demo the use of list variable Author: Created: ---> <!--- create the list ---> <CFSET lDepts="Marketing,Operations,Sales,Technology"> <HTML> <HEAD> <TITLE>Create The List</TITLE> </HEAD> <BODY> <P>We have created a list with four elements. Follow the link to pass this list to another page.</P> <P>The List looks like this:</P> <!--- output the list ---> <P><CFOUTPUT>#lDepts#</CFOUTPUT></P> <P><CFOUTPUT><A HREF="List2.cfm?list=#lDepts#">Next Page</A></CFOUTPUT></P> </BODY> </HTML> -
Save the file as List1.cfm in your Examples\Step10 folder. -
Now we will create the page with the drop-down menu. Open your text editor and type the code in Listing 10.2 or you can open the List2.cfm file from the CompletedFiles\Examples\Step10 folder. Listing 10.2 List2.cfm [View full width] <!--- File: List2.cfm Description: Demo the use of list variable Author: Created: ---> <HTML> <HEAD> <TITLE>Create The List</TITLE> </HEAD> <BODY> <P>Use the list passed for the previous page to create a drop- down menu.</P> <!--- loop through the list to create the menu ---> <FORM> <SELECT NAME="DeptMenu"> <CFLOOP INDEX="x" LIST="#URL.List#"> <CFOUTPUT> <OPTION VALUE="#x#" SELECTED>#x#</OPTION> </CFOUTPUT> </CFLOOP> </SELECT> </FORM> <P>If you store the list in the Server, Application, or Session scope,<BR> you will be able to access the list from any page without having to manually<BR> pass the variable from page to page.</P> </BODY> </HTML> -
Save the file as List2.cfm in your Examples\Step10 folder. -
Browse to the Examples\Step10\List1.cfm page. You should see something similar to Figure 10.1. Figure 10.1. The List1.cfm browser display. -
Follow the link to List2.cfm. You should see something similar to Figure 10.2. Figure 10.2. The List2.cfm browser display.
We have used a URL variable to pass this list value. It is more common to store list values in one of the shared memory scopes. This way, the list variable is available to templates throughout the application without having to explicitly pass the list variable every time. |