So something I’ve done for a number of years now is to parse the FIELDNAMES variable from the FORM scope to process a form submission. I thought I’d blog about it after a co-worker heard me talking about it and didn’t really know what it was. The FIELDNAMES variable is a built-in variable available in Coldfusion on any form’s ACTION page. It holds a comma delimited list of all the form fields that were submitted from the form.
Parsing the FIELDNAMES variable can be a good way to process form data dynamically. It is especially useful for very long forms or forms that change frequently and keeps you from having to update your action page. In the following example, we take a standard form and submit to an action page that inserts our form data into a database:
We need our database with a defined table, a form and an action template. My sample table in MySQL looks like this:
DROP TABLE IF EXISTS `bradtest`.`users`;
CREATE TABLE `bradtest`.`users` (
`id` int(10) unsigned NOT NULL auto_increment,
`fname` varchar(45) NOT NULL,
`lname` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Remember the column names, as we will use those for our form field names. This is because for this to work right, you must use your database column names as your form field names. So create yourself a form, or use mine below. It doesn’t matter how many form fields you have as long as you have a matching column in your table:
<cfform action=”form_process.cfm” method=”post”>
<table>
<tr>
<td>First Name:</td><td><cfinput type=”text” name=”fname” ></td>
</tr>
<tr>
<td>Last Name:</td><td><cfinput type=”text” name=”lname” ></td>
</tr>
<tr>
<td>Email:</td><td><cfinput type=”text” name=”email” ></td>
</tr>
<tr>
<td>Password:</td><td><cfinput type=”password” name=”password” ></td>
</tr>
<tr><td colspan=”2″><input type=”submit” value=”Submit”></td></tr>
</table>
</cfform>
For the last step, just need only parse our results on the action page. The first step is only needed if you are going to insert into a database. I’m going to calculate the number of commas I need seperate items in my SQL statement:
<cfset commas = #ListLen(form.fieldnames)#>
We actually need one less comma than this value, and it doesn’t come into play until later. Next I’m going to duplicate some code just for the purpose of displaying our results. We will use this CFLOOP routine again in our CFQUERY tag:
<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
<cfoutput>
#ListGetAt(form.fieldnames,fieldCount,”,”)# =
#Evaluate(ListGetAt(form.fieldnames,fieldCount,”,”))#
<br>
</cfoutput>
</cfloop>
Notice we are just displaying the raw value of the fieldname element and then we must EVALUATE the same thing to pull the value from the form field posted to our action page. In fact, you can run the template at this point and you should see a list of the form fields, and equal symbol followed by the value submitted in said form field.
To insert the record, we just need to loop our results inside a CFQUERY tag like so:
<cfquery name=”qInsert” datasource=”MyDSN”>
insert into users
(<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
#ListGetAt(form.fieldnames,fieldCount,”,”)#<cfif fieldCount lt #commas#>,</cfif>
</cfloop>)
values
(<cfloop from=”1″ to=”#ListLen(form.fieldnames)#” index=”fieldCount”>
‘#Evaluate(ListGetAt(form.fieldnames,fieldCount,”,”))#’<cfif fieldCount lt #commas#>,</cfif>
</cfloop>)
</cfquery>
Notice I’m running the loop twice, once for my list of column names (remember we made sure that our table has the same column names as we used for our form field names) and then once for the corresponding values. Also key here is the CFIF blocks in each loop to determine if we need a comma. I know I need one less comma than I have form fields so I place one as long as our loop count is less than the number of elements in the FIELDNAMES array.
That’s it, you now have an action form that can process any number of form fields and any changes you make will automatically be accomodated for. Just remember any changes to your database table must be reflected in your form. Here is our working sample:
http://brad.melendy.com/projects/coldfusion/fieldnames/form_data.cfm