Home > Articles

Conditional Processing in ColdFusion Development

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Conditional processing is code that reacts to specified conditions and behaves differently based on those conditions. When application behavior depends on data that is not known at the time of development, such as user data or query information, conditional processing is an important tool. Conditional processing is essential in the development of ColdFusion applications. Learn what tools are available for this, and which are best under specific conditions.

Conditional Statements

Conditional processing occurs when one statement controls the execution of other statements. Although you might normally think of using <CFIF> and <CFSWITCH> to handle conditionals, other statements can also accomplish this task.

To set the stage for examples of these conditional statements, consider a situation often faced by Web developers—dealing with FORM variables that may or may not be passed to the action page. Unless values are supplied for check boxes, radio buttons, and select boxes, they do not pass values to the template specified in the ACTION attribute of the form.

For the following examples, consider a single check box that is used on a form to ask a user whether she wants to receive email about future product releases. The user will check the box for yes and not check it for no. The name of the form control is Email. On the action page, it must conditionally be decided whether the box was checked.

  • For a further discussion of forms and variables used with ColdFusion, see Chapter 9. "FORM Variables."

<CFPARAM>

You can use the conditional statement <CFPARAM> to provide the value "No" to the form control if it does not exist on the action page by using the following statement:

<CFPARAM NAME="Email" DEFAULT="No">
  • For information concerning what prefix could be used with the Email variable, see Chapter 2, "Working with Variables and Expressions."

IsDefined()

You can use the IsDefined() function to do the same job as <CFPARAM> by using the following code:

<CFIF NOT IsDefined("FORM.Email")>
<CFSET FORM.Email="No">
</CFIF>

Hidden Form Controls

You can also address this problem on the actual form by including a hidden form control passing the value No associated with the form control Email. This way, you ensure that the variable will be defined on the action page.


Caution - If you use the hidden form control method, you must programmatically compensate for the event when the check box is checked. This results in both Yes and No being associated with the form control as a list of values.


  • For more information on forms, see Chapter 9, "Form Variables."

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

There are currently no related podcasts. Please check back later.

There are currently no related blogs. Please check back later.

There are currently no related titles. Please check back later.

Informit Network