Eval.uating Variables ColdFusion Help

At this point, you have been able to do quite a bit more with ColdFusion than you could do with basic HTML, but there is still much more that you can do. Although you have been able to provide minimal feedback to users in our quiz example, you haven’t really been able to tell them how well they are doing. You haven’t been able to evaluate the content of any variables. Being able to evaluate variables enables you to:

• Compare the value of one variable against another
• Execute a set of code based on the value of a variable or comparison of variables
• Account for nonexistent variables
• Control flow and display of your template
• Abort the processing of a template based on the value of a variable In this section you will expand the quiz application to provide valuable feedback to the user about their quiz results control what is displayed in the template based on the user’s quiz results and prevent errors by accounting for nonexistent variables.

Controlling the Display of Your Template Using CFIF

You will.now use basic ColdFusion logic to evaluate the user’s answers. If the user’s answers correct then you will display a congratulatory statement. If they are incorrect you will ask the user to try again. To create this interaction you need to evaluate whether the user_answer variable is equal to the correct_answer variable. You can accomplish this by using CFIF statements

The CFIF statement has both a beginning and ending tag, and may also include the CFELSE and CFELSEIF tags. The format for using CFIF is as follows:

<CFIF some_expression evaluates to true>
Do this
<CFELSEIF some_other_expression evaluates to true>
Then do this
Or do this

So, in your qui z_resul ts. cfm file, you can evaluate whether the user’s answer is the same as the correct answer by modifying your code as shown in Listing 2,7.

.<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Trans;t;onal//EN’>
<TITLE>Qu;z Results</TITLE> ~
<DIY ALIGN=’center’>
<HR><B>Qu;z Results!</B><HR>
<B>Thank you for taking my short qu;z!</B><P>
<CFIF user_answer IS correct_answer>
<B><FONT COLOR-‘#008000’>Congratulat;ons! !!
You are correct!</FONT></B><P>
I’m sorry, but that ;s the wrong answer.
Please ‘try aga;n.</FONT></B><P>
<B>The question was:</B> #quest;on#<BR>
<CFIF user_answer IS NOT correct_answer>
.<B>Your answer was:</B> #user_answer#<BR>
<B>The correct answer ;s:</B>
<B>You took the quiz on:</B> #today#<P>
<DIY ALIGN=’center’>
<I><A HREFa’home.cfm’>Take the quiz aga;n?</A></I>

Now reload your home  cfm file and take the quiz. v\’hen you select the correct answer you should be congratulated. When you select the wrong answer you should be asked to try again and the correct answer should be hidden from you (see Figure 2.5).



You did four things in this file to create-this interaction:

1. Using CFIF r you evaluated whether user_answer was equal to correct_answer. If it evaluated to be Trve, then the next set of HTML and Cold Fusion code was processed up until the CFELSEtag. The code following the CFELSEtag and up until the end jCFIF tag was ignored because H didn’t evaluate to True. The opposite would be true if your initial CFIF stateme:,-t evaluated to False.
2. You added color to increase the dramatic effect of correct or incorrect answers.
3. You displayed the user_answer variable only if the user had chosen the incorrect answer.
4. You displayed the correct_answer variable only if the user had chosen the correct answer. This way a user who chooses the incorrect answer can still try again without seeing the correct answer first

Accounting for Nonexistent Variables

Now that you are able to evaluate variables, you are also able to determine whether they exist. If a variable does not exist, but Yl.)urtemplate calls that variable. ColdFusion will produce an error. So far in the application, this has not been a problem. But this becomes a problem if someone decides to link to y(‘ll.lrqui z_resul ts. cfm file directly without first going to the home. cfm fl!.

Try opening the URL http://127 . 0.0 .1/sybex/qui zJesul ts. cfmand you will see an error. The reason is that parameters have not been passed to the template through the URL. Even though it is unlikely that anyone will try to access this template directly. it is a possibility, and therefore you must account for it.

To account for nonexistent variables, you need to first evaluate whether the variables exist in the template. You can do this using CFIF and the ColdFusion function IsDefined.

<CFIF NOT IsDefined(“user_answer’) OR
NOT IsDefined(‘correct_answer’) OR
NOT IsDefined(‘question”) OR
NOT IsDefinedC’today’»
<DIV ALIGN=’center’>
<HR><B>Wel come !</B><HR>
<I><A HREF=’home.cfm”>Please take my quiz!</A></I>

Now if you load http://l27 . 0.0 .1/sybex/qui zJesul ts. cfm, you will See a link back to the main quiz page instead of an error (see Figure 2.6). The code you just added to the qui zJesul ts. cfm templat~ checks whether any of the four variables are missing. If even one is missing, the processing of the template is aborted. Whenever you want to abort the processing of a template, you can use the Coldf’usion tag CFABORT.



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

Share the Story

Back to Top
Share This