Looping Over Dynamic Value Form Fields
Posted by Stephen Weyrick | Tags: coldfusion
After posting on cftips.net for six months, I decided to create my own blog so that I could post my own entries and create my own plugins for mango blog. In my first blog post, I am going to talk about looping over dynamic values in a form field. I have never done this before, and I thought that it would be something fun to learn in my free time when I'm not under the gun at work.
The first task that I performed was looping over values and types in my database. I am not going to talk about this, because it is relativily simple. You just write a query to grab the values and types stored in your database and loop over them as form inputs. In this example I used check boxes.
After dynamically generating form values, you need to loop over them so that you can update or insert based on a certain criteria. The code below shows how to do this. First I start my loop and then filter out values that I don't need for my update ,delete, or insert. A great way to see the form struct is to cfdump it when its posted <cfdump var="#form#/> . Once you have the data in an array, you can loop over a query to check whether the values exist. I'm not sure if there is an easier way to do this, but my method seemed to work just fine.
Code to store form values:
<cfloop item="key" collection="#form#">
<cfif ISNUMERIC(#form[key]#) AND (#key# NEQ "UserID")>
<cfset myArray[x] = #form[key]#>
<cfset x = x + 1/>
</cfif>
</cfloop>
2 responses so far ↓
Leave a Comment