Handling Form Variables Dynamically - Part Three
Author: Andrew MullerIn part two of this series we examined the fieldnames variable that is returned in the form scope with every form submission.
Looping over that list allowed us to reference each of the form elements as we outputted values to the screen.
While this appears to be valuable to the developer there is one problem that we need to address programmatically to ensure that we've used all form elements in our code.
Some form elements that contain no value, like unchecked checkboxes, are often best handled on the action page with the use of CFPARAM which tests for the existence of a variable, creating it if not defined.
While CFPARAM places values in the form scope, it does not insert into the fieldnames form variable. This is a list of only those variables within the form scope submitted to the action page.
The form scope is now a ColdFusion data structure, a feature introduced with the release of ColdFusion 4.5.
This means that like other structures we can loop over all the elements of the form using a collection loop.
#i#: #evaluate("form.#i#")#
The sample code above will loop over all the elements of the form scope, printing the name of the element in bold, then the element's value using the Evaluate function to resolve it.
Since the fieldnames variable is standard in all forms we will need to suppress it otherwise we will also print it to the screen as well. The sample code below will suppress that variable.
#i#: #evaluate("form.#i#")#
Structure functions can also be applied to the form scope if required.
One example would be the StructIsEmpty function which could be used to test if there are values within the scope.

