- Understanding Variables
- Data Types
- Variable Scopes
- Naming Variables
- Setting Values
- Retrieving Values
- Accessing CGI Variables
- Example 2.1: Using CGI Variables
- Passing Values
- Example 2.2: Passing Variables Via the URL
- Makeover Exercise
- Summary
Example 2.2: Passing Variables Via the URL
In this example, we are going to use two pages to demonstrate how to pass variables via a URL.
-
Open your editor and type the code shown in Listing 2.2, or you can open the Page1.cfm file from the CompletedFiles\Examples\Step02 folder.
-
Save the file as Page1.htm into your Examples\Step02 folder.
-
In your text editor create a new file and type the code shown in Listing 2.3, or you can open the Page2.cfm file from the CompletedFiles\Examples\Step02 folder.
-
Save the file as Page2.cfm into your Examples\Step02 folder.
-
Open a browser and browse to Page1.htm. Your URL should be something similar to http://localhost/Examples/Step02/Page1.htm. You should see a page similar to the one shown in Figure 2.3.
-
Follow any link on Page1.htm. Notice the URL variable present in the browser's address bar. You should see a page similar to the one shown in Figure 2.4.
-
The variable is pulled from the URL and is used in the <CFOUTPUT> statement.
Listing 2.2 Page1.cfm
<! File: Page1.htm Description: Demonstrates passing URL variables Author: Created: > <HTML> <HEAD> <TITLE>Pick a color any color</TITLE> </HEAD> <BODY> <H2>What is your favorite color?</H2> <A HREF="Page2.cfm?Color=Blue">Blue</A><BR> <A HREF="Page2.cfm?Color=Red">Red</A><BR> <A HREF="Page2.cfm?Color=Green">Green</A><BR> <A HREF="Page2.cfm?Color=Yellow">Yellow</A> </BODY> </HTML>
Listing 2.3 Page2.cfm
<! File: Page2.cfm Description: Demonstrates the use of URL scope variables Author: Created: > <HTML> <HEAD> <TITLE>That is so cool</TITLE> </HEAD> <BODY> <H2>That is so cool!</H2> <H2><CFOUTPUT>#URL.Color# is my favorite too.</CFOUTPUT></H2> <A HREF="Page1.htm"><<back</A> </BODY> </HTML>