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:

Next: Conclusion >>
More ColdFusion Articles
More By Jackie Kong