Getting Started With ColdFusion Templates and Parameters
Because ColdFusion is template driven, it's very easy to learn. In this article Jackie shows us how to construct our own ColdFusion templates. He also shows us how to work with both URL and form parameters.
Getting Started With ColdFusion Templates and Parameters - Passing Parameters Through the URL (Page 3 of 5 )
To create more interactive pages, sometimes you need to pass information (or parameters) from one ColdFusion template to another. There are basically 2 methods to do so:
Pass through the URL
Pass through a form
URL Parameters To pass a parameter to another ColdFusion template through a URL, you separate the URL from your parameter with a question mark (?). The question mark is followed by a "parameter name = value" pair. This is the standard syntax for passing parameters with any web browser and any web server, and its syntax looks like this:
Let's make a quiz that passes the user's answer through the URL. Enter the following into your editor and save the file as quiz.cfm in the learncf folder:
<html> <body> <h1>Today's Quiz</h1> What am I learning now? <ol> <li><a href=quiz_answer.cfm?answer=ASP>ASP</a> <li><a href=quiz_answer.cfm?answer=PHP>PHP</a> <li><a href=quiz_answer.cfm?answer=ColdFusion>ColdFusion</a> <li><a href=quiz_answer.cfm?answer=JSP>JSP</a> </ol> </body> </html>
You may notice that all the links are pointing to quiz_answer.cfm with the parameters appended to the end of the URL. The parameters differ from one link to another. Now, we need to create the quiz_answer.cfm file, which will handle the answer page. Enter the following code and save it as quiz_answer.cfm in the learncf folder:
<html> <body> <h1>Thank you!</h1> What am I learning now?<br> <cfoutput> Your answer was <b>#url.answer#</b> </cfoutput> </body> </html>
Point your browser to http://localhost/learncf/quiz.cfm and click on an answer. You will see something like this:
What we have done is passed the value of a parameter from one page to another. You access the value by using #url.answer#, where answer is the parameters name. The url prefix you use here is the scope/type of the parameter.
How would you go about passing more than one parameter? As I said earlier, it's very easy. You simply separate them with an ampersand (&). The following code will pass the correct answer to the quiz_answer.cfm page by adding &correct_answer=... to the links.
<html> <body> <h1>Today's Quiz</h1> What am I learning now? <ol> <li><a href=quiz_answer.cfm?answer=ASP&correct_answer=ColdFusion>ASP</a> <li><a href=quiz_answer.cfm?answer=PHP&correct_answer=ColdFusion>PHP</a> <li><a href=quiz_answer.cfm?answer=ColdFusion&correct_answer=ColdFusion>ColdFusion</a> <li><a href=quiz_answer.cfm?answer=JSP&correct_answer=ColdFusion>JSP</a> </ol> </body> </html>
The code below is a modified version of quiz_answer.cfm that handles the url.correct_answer parameter as well:
<html> <body> <h1>Thank you!</h1> What am I learning now?<br> <cfoutput> <cfif url.answer eq url.correct_answer> Congratulations, you answered correctly. <cfelse> Ops, you are wrong. </cfif> <br> Your answer was <b>#url.answer#</b> </cfoutput> </body> </html>
The code shown above uses the <cfif> tag to check whether the user clicked on the correct answer. Depending on whether the answer is correct or not, the code will display a corresponding message. <cfif url.answer eq url.correct_answer> effectively evaluates whether url.answer was equal to url.correct_answer. eq is a comparison operator and represents is equal to in ColdFusion: