passing data between coldfusion templates ColdFusion Help

Now that you have created your first ColdFusion template you are ready to learn how to pass values or parameters between two Coldf’usion templates. This chapter will provide a basic overview of how to pass parameters between ColdFusion templates using URLs and forms. You will also learn how to control the flow of your document through the use of the CFIF tag. This chapter builds upon the HTML knowledge you already have and shows you how much more can be done with it using ColdFusion.

Passing ColdFusion Parameters An Overview

If you’re using plain HTML there is no way to pass data between pages or store information in variables. This results in a static Web site without much interactivity. The most you can do on an interactive basis is allow the user to navigate to other pages submit forms (which can only be sent unformatted to an e-mail address) and send an e-mail (provided the user’s browser is configured appropriately)

Once you can pass data between HTML pages and store the data in riables you can create many more interactive processes with your Web site For example just some of the things you can do include the following

• Create a form that requests information from users then display that information on the next page while also storing the information in variables for later use
• Program a quiz, evaluate user answers, and keep track of scores
• Track which pages a user has visited on your site
• Create a game of tick-tack-toe or some other puzzle
• Redirect users to different locations based on variable contents

You may already have worked with other technologies that enable you to create these types of interactions For example JavaScript can be used to pass data between HTML pages. You can also use Common Gateway Interface (CGI) programming Active Server Pages, and a whole host of other technologies. ColdFusion though is generally much easier to work with, especially for someone who has never programmed. Also Cold-Fusion offers advantages over all these technologies because it is easier to learn, easier to use and allows for database interaction (which will be covered in the next chapter). In this chapter you will create a short quiz using variables URL parameters and form parameters.

Passing Parameters thrugh the URL

You are already familiar with how to create a basic hypertext link that enables the user to navigate to various pages on the Web. In the next example, you will be using hypertext links to allow for user navigation while also passing parameters to the next Cold-Fusion template. A parameter is the value of an assigned variable that is passed to another template. This can be done through the URL or a form.

Creating a Basic ColdFusion URL

To pass a parameter to another template, you must separate the standard URL from the parameters you are passing with a question mark (?). The ? is then followed by the name of the parameter equal to the value of the parameter-in this case, answer-1985. The syntax
for passing a parameter to the next template is as follows:

<A HREF~’name_of_template.cfm?var;ablel-valuel’>

If you want to pass more than one parameter you must separate each with an ampersand (&), as in the following example:

<AHREF-‘name_of_template.cfm?var;ablel-valuel&var;able2 value2’>

In the. home. cfm file that you created in Chapter 1 “Creating Your First ColdFusion Template,” you can now add the code in Listing 2.1

FIGURE 2.1

FIGURE 2.1

<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD ‘HTML 4.0 Transitional//EN’)
<HTML)
<HEAD)
<TITLE>Quiz Results</TITLE)
</HEAD> ~
<BODY BGCOLOR-“FFFFFF’>
<DIV ALIGN-‘center’>
<HR><B>Quiz Results!</B><HR>
<B>Thank you for ta~ing my short quiz!</B><P>
</DIV>
<CFOUTPUT>
<B>The question was:</B>
In what year was ColdFusion introduced?<BR>
<B>Your answer was :</B> furl. user _answer#<BR>
<B>The correct answer is:</B> 1995<P>
</CFOUTPUT>
<DIV ALIGN-‘center’>
<I><A HREF-‘home.cfm’>Take the quiz again?</A></I>
</DIV>
</BODY>
</HTML>

Now reload the home. cfm file in your browser window and select the first answer 1985. You should be directed to the file qui zJesul ts. cfm, which should look like Figure 2.2.

FIGURE 2.2

FIGURE 2.2

You have just passed your first parameter from one template to another through the URL.lf you look at the URL in the browser’s location, you will notice tha~ it also contains the passed parameter. Cold Fusion reads this URL when the template is beiDg processed and assigns all the parameters to variable names in the following tQJ’ll\at: URL•parameter_name.You can then access the generated variable later in the t¥alplate, which is exactly what you did. When you coded which year the ~ choIe as ‘ furl. user_answer'” you accessed the variable URL•user_answer. TI1ts ~riable was generated by ColdFusion when it parsed the parameter in the URL.

Other types of variables are listed in Table 2.1. You will be Iearning about URL and form variables later in this chapter.

TAB LE 2. 1:

TAB LE 2. 1:

This was of course a simple example. You passed a parameter from one template to another and. then displayed the generated variable to the user. It would Iife much more useful if you could evaluate the value of that parameter and then display different messages based on whether the answer was correct You will learn how to do exactly this later in this chapter

Working with Multiple Parameters

In this section you will expand on the previous example by passing four parameters with different characteristics through the URL Before modifying your code it is important to look at each of the parameters that you will pass

In the last example you passed only the user_answer parameter. The only real benefit gained by doing so was to display the answer to the user on the next page. Everything else on the qui z_resul ts. cfm file was hard-coded. But what if your ultimate goal is to create a quiz with randomly generated questions or multiple questions on the first template? If that is your goal, then you cannot hard-code the question and the correct answer on your second template (as was done in the previous example) because the question and answer could be different each time.

You will be adding an additional quiz question to your home. cfm file to illustrate the preceding point. This will enable you to pass different quiz question variables through the URL You will be modifying your code so that the following variables will be passed through each of the URLs

question The full text of the question the user has chosen
user_answer The answer chosen by the user
correct_answer the correct answer
today The variable today that you created in Chapter 1

First, you need to create your variables in your home. cfm file. You already created the today variable in Chapter 1. The user _answer variable will be selected by the end user as part of the URL as demonstrated in the preceding section. To create the quest on and correct_answer variables and to add an additional question modify the code in your home. cf!” ale as shown in Listing 2.3

<!OOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
<HTML>
<HEAD>
<TITLE>My Home Page</TITLE>
</HEAD>
<BODY BGCOLOR-‘#FFFFFF’> ‘.
<DIV ALIGN-‘center’>
<HR><B>welcome to My Home Page!</B><HR>
<B>Today’s date is:</B><BR>
<CFOUTPUT>
<!—
The foll~ng variable, ‘today’, displays the current
date in the format: Tuesday, September 15, 1998
How() returns the current timestamp (date & time)
dddd – full day of week
mmmm – full name of month
d – day of month
yyyy – year in four digits
—>
<CFSET today – OateFormat(Now(), ‘dddd, mmmm d, yyyy’»
<I>ftoday'</I>
</CFOUTPUT>
</DIY>
<!— The following is code for my first quiz question —> “.
<CFSET question1 – ‘In-what year was ColdFusion introduced?’>
<CFSET ~orrect_answer1 – 1995> .”
<P><HR><B>Today’s 1st question is:</B>
<BR><I><CFOUTPUT>lquestion1t</CFOUT~/I>
<OL>
<LI><A HREF-‘quiz_results.cfm?user_answer-198S’>198S</A>
<LI><A HREF-‘quizJesu 1ts.cfm?user_answer-1990.’>1990</ A>
<LI><A HREF-‘quiz_results.cfm?user_answer-199S’>199S</A>
<LI><A HREF-‘quizJesults.cfm?user_answer-1998:>1998</A>
</OL>
<!— The foll~ng is code for my second quiz question —>
<CFSET question2 – ‘Who developed the first version of ColdFusio~?’>
<CFSET correct_answer2 – ‘J.J. Allaire’)
<P><B>Today’s 2nd question is:</B>
<BR><I><CFOUTPUT>’question2′</CFOUTPUT></I>
<OL>
<LI><A HREF-‘quizJesuh~CTm?us;r _answer-‘ Kristin Motlagh’ ‘>KristiriMotlagh</A>
<LI><A HREF-‘quiz_results.cfm?user_answer-‘J.J. Allaire”>J.J. Allaire</A>
<lI><A HREF-‘quiz_resu1ts.cfm?user_answer-‘Arman Oanesh”>Arman Oanesh</A>
<~L~I><A HREF-‘quiz_results.cfm?u’s.er_answer-‘Gaio , Gonzalez”>Gaio G- onzalez</A>
</BOOY>
</HTML>

Here is what you just did to your template:

1. Added a comment line explaining where the code for each quiz question started
2. Set the questi on! and correct_answer! variables for question 1
3. Changed from displaying a hard-coded question to displaying the question! variable
4. Added code for the second question and repeated steps 1-3for question

Now that you have all your variables set, you are ready to add them as parameters to the URLs in your home. cfm file. Let’s add each parameter one at a time to the first URL in your first question. Your current URL format looks like this:

<A HREF-‘qu;z_results.cfm?user_answer-198S’>198S</A>

You are already passing the user_answer parameter. This parameter is hard-coded into \he~. Now you can add the correct_answer parameter. But, because the correct_answer parameter is not hard-coded, and its value is equal to the ColdFusion variable correct_answer! that you set earlier in the template, you will need to output it in the URL using CFOUTPUT tags. Here is the resulting code

<CFOUTPUT><A”HREF-‘qu;z_results.cfm?user_answer-198S&cor~ect_answer

You’re not qU\te finished yet. You now need to complete the same set of steps for the other three URLs in the first question and the rest of the URLs in the second question. Remember that while you are modifying your code, you need only one CFOUTPUT tag set to surround all of your ColdFusion variables. You do not need a separate CFOUTPUT tag set for each variable.

Your final file should look like Listing 2.4.

<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
<HTMl>
<HEAD>
<TITLE>My Home Page</TITLE>
</HEAD>
<BODY BGCOlOR-“FFFFFF’>
<DIY ALIGN-‘center’>
<HR><B>Welcome to My Home Page!</B><HR>
<B>Today’s date is:</B><BR>
<CFOUTPUT>
<1—
The follow1ng variable, ‘today’, displays the current
date in the .format: Tuesday, September IS, 1998
Now() returns the current timestamp (date & time)
dddd ~ full day of week
mmmm – full name of .onth
d – day of month
yyyy – year in four digits
—>
<CFSET today – DateFormat(Now(), ‘dddd, mmmm d, yyyy’»
<I>ftoday'</I>
</CFOUTI’UT>
</DIV>
<1— The follow1ng is code for my fi.rstquiz question —>
<CFSET questionl – ‘In what year was ColdFusion introduced?’>
<CFSET correct_answerl – 1995>
<P><HR><B>Today’s 1st question is:</B>
<CFOUTPUT>
<BR><I>’questionl'</I>
<OL>
<LI><A
HREF-‘quiz_results.cfm?user_answer-1985&correct_answer-fcorrect_answerl~uestion-fqu
estionl’&today-ftoday”>1985</A>
<LlXA HREF-‘quiz_results.cfm?user_answer-199O&correct_answer-t
correct_answerl’&question-‘quest;onl*&today-ltoday”>1990</A>
<LlXA .
HREF-‘qu;z_results.cfm?user_answer-1995&correct_answer-fcorrect_answerlf&quest;on-fqu
est;onl#&today-ftodayl ‘>1995</A>
<LlXA
HREF-‘qu;z_results.cfm?user_answer-1998&correct_answer-fcorrect_answerlf&question-fqu
estionl#&today-ftoday”>1998</A>
</OL>
</CFOUTPUT>
<!— The following is code for my second quiz question —>
<CFSET quest;on2 – ‘Who developed the first version of ColdFus;on?’>
<CFSET correct_answer2 – ‘J.J. Alla;re’>
<P><8>Today’s 2nd question is:</8>
<CFOUTPUT>
<8R><I>’quest;on2′</I>
<OL>
<LlXA HREF-‘qu;z_results.cfm?user_answer-‘Kr;st;n Motlagh’&correct_answer-
‘correct_answer2f&question-fquest;on2’&today-‘today”>Kristin Motlagh</A>
<LlXA HREF-‘qu;z_results.cfm?user_answer-‘J.J.
Alla;re’&correct_answer-fcorrect~answer2f&question-fquestion2*&today-,todayl’>J.J.
Allaire<lA>
<LI><A HREF-‘qu;z_results.cfm?user_answer-‘Arman Danesh’&correct_answer-
‘correct_answer2’&question-‘question2*&today-ltodayl’>Arman Danesh</A>
<LI><A HREF-‘quiz_results.cfm?user_answer-‘Gaio Gonzalez’&correct_answer-
‘correct_answer2’&question-fquestion2’&today-‘today*’>Gai0 Gonzalez</A>
</OL>
</CFOUTPUT>
</BOOY>
</HTML>

To test your changes, you also need to make some changes to your second template quiz  efm. Because you are no longer hard-coding any of the quiz data on the second template, you need to remove the quiz question and answer from the second template and replace them with the variables generated from the parameters passed through the URL. The variables generated are as follows:

• URL.user_answer
• URL.correet_answer
• URL.question
• URL.today

Modify your qui zJesu 1ts. efm as shown in Listing 2.5.

<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN”>
.I
<HTML>
<HEAD>
<TITLE>Quiz Results</TITLE>
</HEAD>
<BODY BGCOLOR-‘#FFFFFF’>
<DIV ALIGN-‘center’>
<HR><B>Quiz Results!</B><HR>
<B>Thank you for taking my short quiz!</B><P>
</DIY>
<CFO\.iTPUT>
<B>The question was:</B> #question#<BR>
<B~Your answer was:</B> #user_answer#<BR>
<B>The correct answer is:</B> #correct_answerl<BR>
<B>You took the quiz on:</B> #today#<P>
</CFOUTPUT>
<DIY ALIGN-‘center’>
<I><A HREF-‘home.cfm’>Take the quiz again?</A></I>
</DIY>
</BDDY>
</HTML>

In this file, you added the date the quiz was taken and you changed the question and correct answer.from hard-coded values to the output of variables generated from the URL. Now go to your browser and reload the home. efm file (see figure 2.3). You will notice that the value displayed in the status window when you put your mouse over the URL has changed. Select the first answer to question 1

FIGURE 2.3

FIGURE 2.3

What did you see? Well  hopefully you received your first ColdFusion error. If you didn’t receive an error then recheck your code to make sure it is correct. The error should look something like this:

Error Diagnostic Information
Error resolving parameter TODAY

ColdFusion was unable to determine the value of the parameter. This problem is very
likely due to the fact that either

1.You have misspelled the parameter name, or
2.You have not specified a QUERY attribute for a CFOUTPUT CFMAIL or CFTABLE tag.

That’s a pretty long error message with a lot of information that, at this point, may not make a lot of sense. But it does provide all the information you need to figure out the problem. Lcok at the entire text of the error for a minute or so and see if you can figure out the problem for yourself.

You know from the error message that ColdFusion had a problem processing the today parameter When the Cold Fusion processor came across the variable today it searched for the value (the one you passed through the URL) but could not find it. If you look at the last line of the error message you will see that the today parameter was never passed. In fact, the quest; on parameter was not completely passed either. This is because you cannot pass certain characters such as spaces, through the URL. When you passed the quest on parameter through the URL its value contained spaces and the browser did not understand how to process it. As a result the URL was truncated at the first space received in the URL

To preserve the value of parameters passed through the URL you must convert the “illegal” characters to characters that the browser can better interpret. Cold Fusion enables you to do this with the function URLEncodedFormat.

in ,Chapter 1 you learned a bit about ColdFusion functions. Here, the URLEncoded- Format function is used similarly to the way that the DateFormat function was used in Chapter 1. Remember that functions perform an action and return a result. You must apply the URLEncode~Format function to the parameter you are passing in the followingmarmer:

Modify all the links in your home. cfm file as demonstrated in the preceding code. is important that you always apply the URLEncodedJor’mat function to each of the parameter~ (even if they are hard-coded] because they could possibly be changed and contain illegal characters at some time in the future. You will also notice that the hard-coded answers for the second question contain spaces and will therefore cause an error if you do not apply the URLEncodedFormat function to them

Now reload the home, cfm file in your browser and test the links. When you link to the qui z_resu 1ts. cfm file, it should look similar to Figure 2.4.

FIGURE 2.4

FIGURE 2.4

Your final template should look like Listing 2.6.

Listing 2.6
<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
<HTML>
<HEAD>
<TlTLE>My Home Page</TITLE>
</HEAD>
<BODY BGCOLOR-‘#FFFFFF’>
<DIV ALIGN~’center’> ,
<HR><B>Welcome to My Home Page!</B><HR>
<B>Today’s date is:</B><BR>
<CFOUTPUT>
<!—
The following variable, ‘today’, displays the current date in the format:
Tuesday, September 15, 1998
Now() returns the current timestamp (date & time)
dddd x full day of week
mmmm ~ full name of month
d -.~ay of month
yyyy – year in four digits
—>
<CFSET today – DateFormat(Now(), ‘dddd, mmmm d, yyyy’»
<D#today'</I>
</CFOUTPUT>
</DlY>
<!— The following is code for my first quiz question —>
<CFSET question1 – ‘In what year was ColdFusion introduced?’>
<CFSET correct_answer1 • 1995>
<P><H~<B>Today’s 1st question is:</B>
<CFOUTPUT>
<BR><I>’question1′</I>
<OL>
<LI><A HREF-‘quiz_results.cfm?user_answer-
IURLEncodedFormat(1985)’&correct_answer-IURLEncodedFormat(correct_answer1)#&question=
IURLEncodedFormat(question1)l&today-lURLEncodedFonmat(today)”>1985</A>
<LI><A HREF-‘quiz_results.cfm?user_answer-lURLEncodedFormat(1990)
‘&correct_answer-IURLE~codedFormat(correct_answer1)#&question-lURLEncodedFormat(quest
ionl)’&today-#URLEncodedF~rmat(todaY)#’>1990</A>
<LI><A HREF=’quiz_results.cfm?user_answer-#URLEncodedFormat(l995)
#&correct_answer=#URLEncodedFormat(correct_answerl)#&question=#URLEncodedFormat(quest
ionl)#&today=#URLEncodedFormat(today)#’)1995</A>
<LI)<A HREF=’quiz_results.cfm?user_answer=#URLEncQdedFormat
(1998)#&correct_answer=#URLEncodedFormat(correct_answerl)#&question-#URLEncodedFormat
(questionl)#&today-#URLEncodedFormat(today)#’)1998</A>
</OL>
</CFOUTPUT>
<P><B)Today’s 2nd question ;s:</B)
<CFCUTPUT>
<BR><I)#question2#</I)
<!— The following is code fo~ my second quiz question —)
<CFSET quest;on2 = ‘Who developed the first version of ColdFusion?’)
<CFSET correct_answer2 = ‘J.J. Allaire’)
~L> .
<LI><A HREF=’quiz_results.cfm?user_answer~URLEncodedFormat(‘Kr;st;n
Motlagh’)#~orreccanswer~URLEncodedFormatJcorreccanswer2)#&quest;on-#URLEncodedFor
mat(question2j#&today=#URLEncodedFormat(today)#’)Kristin Motlagh</A>
<LI)<A HREF=’quiz_results.cfm?user_answer=#URLEncodedFormat(‘J.J.
Allaire’)#&correct_answer=#URLEncodedFormat(correct_answer2)#&question-#URLEncodedFor
mat(question2)#&today-#URLEncodedFormat(today)#’)J.J. Alla;re</A>
<LI)<A.~REF=’quiz_results.cfm?user_answer-#URLEncodedFormat(‘Arman
Danesh’)#&correct_answer=#URLEncodedFormat(correct_answer2)#&question-#URLEncodedForm
at(question2)#&today-#URLEncodedFormat(today)#’)Arman Danesh</A>
<LI)<A HREF=’qu;z_results.cfm?user_answer-#URLEncodedFormat(‘Ga;o
Gonzalez’)#&correct_answer-#URLEncodedFormat(correct_answer2)#&quest;on-#URLEncodedFo
rmat(quest;on2)#&today=#URLEncodedFormat(today)#’>Gaio Gonzalez</A>
</OL) –
</CFOUTPUT>
</BODY>
</HTML>

In this section, you have learned to pass multiple parameters through the URL. Some rules to remember when you pass parameters through the URL are:

• You must use a question mark (7) to separate your standard URL from the parameters you are passing.
• You must separate each passed parameter with an ampersand (&).
• You must always use URLEncodedFormat when passing parameters through the URL to avoid problems with illegal characters.
• You must always use pound signs (#) to surround variables included in a link.
• If you include variables in your link, you must surround the link with CFOUTPUT tags.

Posted on November 14, 2015 in Passing Data Between ColdFusion Templates

Share the Story

Back to Top
Share This