How can I dynamically find what form fields have been posted into a page?
ColdFusion provides two easy ways to obtain a list of all form variables that have been posted to a page.
The first way is to use the form.fieldNames variable. The form.fieldNames variable is automatically available to any ColdFusion template that has received a form post and contains a comma-delimited list of form-field names that have been posted to the current template.
The second way is to use the form structure. The form structure is a special ColdFusion structure that contains each form-field name and its associated value. The following is a code sample for displaying the content of the form structure. Note how the form.fieldNames is filtered out of the result set:
<cfif theField is not "fieldNames">
<cfoutput>
#theField# = #form[theField]#<br>
</cfoutput>
</cfif>
</cfloop>
This question was written by Jeremy Petersen.
It was last updated on April 17, 2006 at 9:35:54 AM EDT.
CFML Referenced
Categories
Comments
Comment made by Justin Judd on April 16, 2006 at 2:59 AM
Don't forget that ColdFusion creates form.fieldNames automatically, so if you want to see what was actually posted by the browser, you can use the following (instead of the second example):
<cfloop collection="#form#" item="theField"> <cfif theField is not "fieldNames"> <cfoutput> #theField# = #form[theField]#<br /> </cfif> </cfloop>
Comment made by Raymond Camden on April 16, 2006 at 8:35 AM
I disageee. The first example does mention form.fieldnames. I think the second example is good as it shows that the Form scope is a structure in CF.
Comment made by Jeremy Petersen on April 17, 2006 at 9:40 AM
Cleaned up the entry to focus on the form structure using form.Fieldnames filtering as shown by Justin.
Comment made by irvin on November 5, 2006 at 6:39 PM
What about looping over the fieldnames list?
<cfloop list="#form.fieldnames#" index="x"> <cfoutput> #x# = #form[x]#<br> </cfoutput> </cfloop>
Comment made by Raymond Camden on November 5, 2006 at 10:33 PM
It is a matter of taste I guess. Your code still uses Form as a struct, but it just gets the list from form.fieldnames. To me - I'd say use just struct functions.