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