Home arrow ColdFusion arrow Page 4 - 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 Forms
(Page 4 of 5 )

On the last page we learnt how to pass parameters through the URL. This is an easy and method and is most suitable for passing small amount of data. However, one problem with this method is that the parameters you pass are visible in the address bar of the browser (for instance, when quiz_answer.cfm is displayed). A solution for this is to pass the parameters through a form.

Pass Through a Form
Passing parameters through a form involves basic HTML form elements such as <form>, <input type=text>, <input type=hidden> and <input type=radio>.

Now, let's convert our quiz to pass variables through a form instead of the URL. This basically involves assigning an <input type=radio> element to each quiz answer. Enter the following code into your text editor and save the file as quiz_form.cfm.

<html>
<body>
<h1>Today's Quiz</h1>
What am I learning now?
<form method="post" action="quiz_form_answer.cfm">
<ol>
<li><input type=radio name=answer value=ASP> ASP
<li><input type=radio name=answer value=PHP> PHP
<li><input type=radio name=answer value=ColdFusion> ColdFusion
<li><input type=radio name=answer value=JSP> JSP
</ol>
<input type=hidden name=correct_answer value=ColdFusion>
<input type=submit>
</form>
</body>
</html>


You need to create another page to check the answer of the question. Enter the following code and save the file as quiz_form_answer.cfm:

<html>
<body>
<h1>Thank you!</h1>
What am I learning now?<br>
<cfoutput>
<cfif form.answer eq form.correct_answer>
Congratulations, you answered correctly.
<cfelse>
Ops, you are wrong.
</cfif>
<br>
Your answer was <b>#form.answer#</b>
</cfoutput>
</body>
</html>


In the quiz_form_answer.cfm file, you may notice that we used a <cfif> statement to check the correctness of the answer. This statement compares form.answer with form.correct_answer to see if they are the same. If they are the same then the user has answered correctly; otherwise he is wrong. As in our URL example, eq in the <cfif> statement is a comparison operator for equality. There are other equality operators, and they are listed below:
  • eq: equal
  • lt: less than
  • gt: greater than
  • lte: less than or equal
  • gte: greater than or equal
  • neq: not equal to
Notice that values of radio buttons and correct_answer (hidden input field) are accessed with #form.xxx#. You can safely leave "form." out, but putting it in will make the page process slightly faster because you are telling ColdFusion that the parameters/variables belong to the form scope of the page explicitly.

Lastly, we need to test our new form-driven questionnaire. To test it, point your browser to http://localhost/learncf/quiz_form.cfm. You should see something like this:


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 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials