How Are URL Variables Created?


How Are URL Variables Created?

URL variables are not created by ColdFusion; they are created when browsers request URLs and specify parameters. To create a URL variable, you must first create a hypertext link like so:

 <a href="index.cfm">Click Me</a> 

URL variables are specified in the query string portion of a URL (the text after a ? character). You add URL variables to the link by first typing a question mark and then adding the name=value pair as follows:

 <a href="index.cfm?FName=Emily">Click Me</a> 

Now when the link is clicked, a variable called FName, with a value of Emily, is passed to the index.cfm page.

NOTE

You can make a hypertext link from an image as easily as you can from text. The same rules for creating URL variables apply to links around images. Just add the name=value pairs to the HTML <a> tag as shown in the preceding example. When the image is clicked, the variables are passed as normal.


Passing Multiple Variables at Once

You can pass multiple variables on the end of one link by separating each name=value pair with an ampersand (&). The following code passes three variables to the index.cfm page:

 <a href="index.cfm?FName=Emily&MInit=B&LName=Kim">Click Me</a> 

CAUTION

You can pass a lot of information in a query string; however, all browsers impose a length limit on URLs. Typically, older browsers allow approximately 254 characters to be passed in a URL; newer browsers' limits are higher. You should use this variable type with care because long URLs can become unsightly if used excessively.


Passing Complicated Strings

Sometimes you need to pass URL-unfriendly characters in your URL variables. For instance, spaces are not allowed in a URL. A ColdFusion function called URLEncodedFormat() is very useful in such a scenario.

URLEncodedFormat() accepts a single parameter, as shown in the following code snippet:

 <!--- using URLEncodedFormat() with a string ---> <cfoutput> <a href="index.cfm?Name=  #URLEncodedFormat("Emily Kim")#">Click Me</a> </cfoutput> <!--- using URLEncodedFormat() with a variable ---> <cfset MyName="Emily Kim"> <cfoutput> <a href="index.cfm?Name=  #URLEncodedFormat(Variables.MyName)#">Click Me</a> </cfoutput> 

NOTE

The <cfoutput> tags are necessary in either case because URLEncodedFormat() is a Cold Fusion function that must be evaluated by the ColdFusion server even if its parameter is a string.

Also note that quotation marks appear around the string but not around the variable name in the function parameter. Quotation marks denote a string, whereas the lack of quotation marks in this case tells the ColdFusion server to evaluate the variable.


In either case, the function converts the space into its ASCII equivalent (in this instance, %20), which allows the variable's value to be passed successfully. Without this function, the part of the string after the space would be lost.

NOTE

When working with some newer browsers, you will often find that the string is passed successfully regardless of whether you use the function URLEncodedFormat(). This is because the browsers are forgiving and convert the special characters for you. However, you should not depend on this feature unless you're sure that your Web audience will be using such browsers.


TIP

When you're passing URL variables, using URLEncodedFormat() is usually a good idea even if you don't think it's necessary. Later, we will discuss dynamically created URL variables, whose values can often be unpredictable.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net