Hypertext Transfer Protocol (HTTP), the underlying protocol used by the World Wide Web, is a stateless protocol. This means that each page request made to a web server is independent from any others and has no information about anything that has occurred previously. Consequently, no information is kept about what data has been entered or what events have occurred prior to the current page request.
The capability to track state, which includes such things as user actions or entered information, is the cornerstone of any modern application. For us to be able to transform our old, static web site into a responsive, data-driven web application, we have to be able to track and pass information from one page to the next.
One of the easiest ways to track information is to store it in a variable and then pass that variable between pages. This can be done in a number of ways. In this step, however, we will focus on using forms and web-page URLs to pass variables.
Passing Values with Forms
If you have been using the Internet for any length of time, you have probably filled out a web form or two. This is one of the most common ways to pass information between web pages. Information entered into a form is packaged up and sent to the web server. The web server then uses a script of some sort to process that information and do something with it, such as email it to the webmaster, pass it to another page, or enter it into a database.
Using forms to pass information will be covered in detail in Step 5, "Using Forms with ColdFusion MX."
Passing Values Via the URL
Another simple way to pass information between web pages is to append it to the end of the URL being requested. This is done by adding a question mark after the filename and then adding a variable name and value. For example, with the URL
we are requesting a page called Details.cfm from http://www.anysite.com, and we are also passing a variable called Name that has been assigned a value of John. Variables passed using this method become variables in the URL scope.
To retrieve this variable and use its value in the Details.cfm template, simply call the variable using its name and the URL scope prefix. For example, the line of code
would look in the URL string for a variable called Name and retrieve its value, in this case John.
If you want to send more than one variable via the URL, you can append additional variable/value pairs to the end of the URL using the ampersand (&) symbol. For example, the URL
would pass our Name variable again as well as another variable called Dept. As you might have guessed, you would access that variable's value in the same way we used our Name variable, by using the variable's name (Dept) and the URL scope prefix as illustrated here:
<CFOUTPUT> Name: #URL.Name# <BR> Dept: #URL.Dept# </CFOUTPUT>
No Space Allowed
When passing information via URL variables, we have to be careful that those variables do not contain spaces. URLs are not allowed to contain spaces, so consequently, if we tack on a URL variable that has spaces in it, we will break that rule. For example, if we use a URL variable, such as TemplateName.cfm?Name=#UsersName# and the #UsersName# variable happens to contain the value John Smith, we could be in trouble. The resulting URL would be TemplateName.cfm?Name=John Smith. This URL could cause us problems, depending on the browser type and version being used.
To get around this problem, we can use the URLEncodedFormat() function. This function encodes URLs and converts non-URL-friendly characters, such as spaces, into URL-safe hexadecimal escape characters. For example, if we use TemplateName. cfm?Name=#URLEncodedFormat(UsersName)# instead of the previous example, we would end up the a URL that looks like TemplateName.cfm?Name=John%20Smith.
To turn that variable back into its original form, you use the URLDecode() function. So, on the TemplateName.cfm template, we could use code like
<CFOUTPUT>Hello, #URLDecode(URL.Name)# </CFOUTPUT>
Hello, John Smith.
Using URL variables is a very common technique for passing information from one template to the next. However, this method should never be used for sensitive information, such as usernames or passwords. Because this information is attached to the URL, it is there for anyone to see. In addition, this information is also recorded if the user happens to take a bookmark of the URL or add it to her favorites list.