Home arrow ColdFusion arrow Page 3 - Getting Started With ColdFusion Templates and Parameters
COLDFUSION

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.

Author Info:
By: Jackie Kong
Rating: 4 stars4 stars4 stars4 stars4 stars / 34
January 02, 2003
TABLE OF CONTENTS:
  1. · Getting Started With ColdFusion Templates and Parameters
  2. · Getting Started With ColdFusion Templates
  3. · Passing Parameters Through the URL
  4. · Passing Parameters Through Forms
  5. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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:

<a href="template.cfm?parameter=value">Click here</a>

If you want to pass more than one parameter, then you need to separate each with the ampersand (&):

<a href="template.cfm?parameter1=value1&parameter2=value2">Click here</a>

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:


blog comments powered by Disqus
COLDFUSION ARTICLES

- Adobe ColdFusion Just Got More RAD
- How to Access a SQL Anywhere Database with C...
- CFXML: Probing XMLDOM in ColdFusion
- Creating a Web Service with ColdFusion: the ...
- CFAjax: What it is and How to Use it
- Querying SQL 2000 Server from ColdFusion
- Introduction to ColdFusion Markup Language, ...
- Introduction to ColdFusion Markup Language
- Databases and Dreamweaver MX 2004, concluded
- Databases and Dreamweaver MX 2004
- Welcome to Coldfusion MX 6.1, concluded
- Welcome to Coldfusion MX 6.1
- What You Must Know About ColdFusion Flow-Con...
- What You Must Know About Operators in ColdFu...
- Everything You Must Know About ColdFusion Va...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials