Passing Parameters through a Form ColdFusion Help

The quiz that you have created still has one major problem. You may already have noticed it: A user can view the correct_answer parameter in the browser’s status bar when they pass their mouse over a link in the home. cfm file. They can also view all the parameters in the location bar of the browser when the qui z_resul t. cfm file is displayed. This is obviously a problem because giving the answers away defeats the purpose of taking a quiz. To solve this problem you can convert your quiz to a form-based application. In a form parameters are passed through a form submission which does not appear in the URL location bar. Unfortunately users can still view the correct answer by looking at the page’s source code. In the next chapter you will learn how to prevent this.

So, for this example you will use radio buttons and a submit button for each question to enable the user to take the quiz. This is a fairly simple example using your background HTML knowledge on forms. You can now modify your home. cfm file to convert each question to a separate form, as shown in Listing 2.9

<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
<TITLE>My Home Page</TITLE>
<DIV ALIGN-‘center’>
<HR><B>Welcome to My Home Page!</B><HR>
<B>Today’s date is:</B><BR>
<!— •
The follow1ng variable, ‘today’. displays the curre-nt date in the format: Tuesday,
September 15, 1998
Now() returns the current timestamp (date & time)
dddd – full day of week
nmnm • full name of month
d • day of month
yyyy’. year in four digits
—> .
<CFSET today· DateFormat(Now(), ‘dddd, nmnm d, yyyy’»
</DIV> ~
<!— The ‘following is code for my first quiz question —>
<FORM ACTION-,quiz_results.cfm’ METHOD-‘post’>
<CFSET questionl – ‘In what year was ColdFusion introduced?’>
<CFSET correct_answer1 – 1995>
<P><HR><B>Today’s 1st question is:</B>

<INPUT TYPE~’hidden’ NAMEa’today’ VALUE-‘#today#’>
<INPUT TYPE-‘hidden’ NAME=’question’ VALUE2’#question1#’>
<INPUT TYPE=’hidden’ NAME=’ correct_answer’ VALUE-‘#correct_answer1#’>
<INPUT TYPE-‘radio’ NAME=’user_answer’ VALUE=’198S’> 1985<BR>
<INPUT TYPE-‘radio’ NAME=’user_an~wer’ VALUE-‘1990′> 1990<BR>
<INPUT TYPEz’radio’ NAME=’user_answer’ VALUE-‘199S’> 1995<BR>
<INPUT TYPE=’radio’ NAMEz’user_answer’ VALUE-‘1998’> 1993<BR>
<P><INPUT TYPE-‘Submit’ VALUE=’Score Question 1!’>
<!— The following is code for my second quiz que~tion —>
<FORM ACTION-‘quiz_results.cfm’ METHOD-‘post’>
<CFSET question2 a ‘Who developed the first version of ColdFusion?’>
<CFSET correct_answer2 – ‘J.). Allaire’>
<P><B>Today’s 2nd question is:</B>
<INPUT TVlE-‘hidden’ NAME-‘today’ VALUE-‘#today#”>
<INPUT TYPE-‘hidden’ NAME-“quescion” VALUE-‘#question2#’>
<INPUT TYPE-‘hidden’ NAME-‘correct_answer’ VALUE-‘# correct_answer2#’>
<INPUT TYPE-‘radio’ NAME-‘user_answer’ VALUE-‘Kristin Motlagh’> Kristin Motlagh<BR>
<INPUT TYPE-‘radio’ NAME-‘user_answer’ VALUE=’J.J. Allaire’> J.J. Allaire<BR>
<INPUT TYPE-‘radio’ NAME-‘user_answer’ VALUE-‘Arman Danesh’> Arman Oanesh<BR>
<INPUT TYPE=’radio’ NAME-‘ user_answer’ VALUE-“Gaio Gonzalez’> Gaio Gonzalez<BR>
<P><INPUT TYPE-‘Submit’ VALUE-‘Score Question 2!’>

You just drastically changed your home. cfm file. In this new version, you are now passing all your parameters through form fields instead of through the URL. This also means that you no longer need to use the URLEncodedFormat function. The user _answer variable is now passed using radio buttons. The correct_answer quest on, and today variables are each passed using hidden fields. In this example, you made each question its own form with its own submit button because you have not provided for the handling of multiple questions in the qui zJesu 1ts . cfm file. Also in the next chapter you will be displaying only one randomly generated question pulled from a database so there is no need to accommodate multiple questions now

Test your quiz. you ln home   efm file should look like Figure 2.7, but your qui zJesul ts .cfm file should look the same as it did before. If you had hard-coded the URL prefix to the variables in eyare assigned to variable names in the following format: form, vari ab1e_name. But as with URL variables you do not need to specify the prefix unless there is a conflicting variable name in the template. Therefore ColdFusion is able to evaluate either form or URL variables in the same way.



In the quiz you have not accounted for a user choosing to submit a form without selecting a radio button. Normally this would result in an error because all parameters would not be passed through the URL. But, because you added code to evaluate whether all variables exist in the quiz_resul ts. efm file the user will not receive an error just a pointer back to the quiz.

Where Do We Go from Here?

In this chapter you created your first ColdFusion application. In creating this application you learned the following techniques and concepts

• Understanding the difference between parameters and variables
• Passing parameters through a URL
• Displaying parameters passed through a URL
• Using the URLEncodedFormat function to preserve URL integrity
• Evaluating variables with the CFIF tag construct
• Using the IsDefi ned function to evaluate the existence of a variable
• Aborting the processing of a template using CFABORT
• Passing parameters using regular HTML forms

In the next chapter you will convert your quiz into a database-driven application that pulls all the quizdata from a database

Posted on November 14, 2015 in Passing Data Between ColdFusion Templates

Share the Story

Back to Top
Share This