Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Form Arrays in ColdFusion?

I have no idea how to handle this in ColdFusion 9, I have a form being submitted (POST) with element checkboxes, called items[].

When I do a <cfdump var="#form#" /> no-problem, I get all the items shown with the proper names like items[] eg:

struct 
ITEMS[] 13,14  
FIELDNAMES ITEMS[] 

however doing a <cfdump var="#form.items[]#" /> results in an error. How do I access the CF9 field values? Somehow loop through it?

I cannot seem to do anything with the array to get the id's out of it? Thoughts? I'm kind of stumped and ColdFusion isn't the easiest language to find examples / references on the net. ;)

Is there a correct way to deal with this? I need to get the ID's out of there so I can reference what lines were checked in the form, so I can follow up with an action.

Thanks!

like image 597
Jakub Avatar asked May 27 '10 19:05

Jakub


People also ask

How do you use an array in ColdFusion?

In ColdFusion, you can create arrays explicitly, by using a function to declare the array and then assigning it data, or implicitly by using an assignment statement. You can create simple or complex, multidimensional arrays. This statement creates a two-dimensional array named myArray.

How do you create a structure in ColdFusion?

Creating structures In ColdFusion, you can create structures explicitly by using a function, and then populate the structure using assignment statements or functions, or you can create the structure implicitly by using an assignment statement.


2 Answers

There's no Form Array’s in ColdFusion. Having '[]' at the end doesn't make it an array. You can access the checkbox values from form scope like this:

FORM["ITEMS[]"]

Dot notation doesn't work 'cause of the '[]'. See: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fb2.html

Values from checkboxes are just comma separated values, which is a List in ColdFusion

To loop through it, use cfloop list=:

<cfoutput>
  <cfloop index="i" list="#FORM['ITEMS[]']#">    
    #i#
  </cfloop>
</cfoutput>

To convert a list to array, use ListToArray(). There are list functions like listGetAt(), but if you're doing lots of random access, it'd be smarter to convert the list into an array first.

Thoughts, I'm kindof stumped and coldfusion isn't the easiest language to find examples / references on the net ;)

  • http://help.adobe.com/en_US/ColdFusion/9.0/Developing/index.html
  • http://learncf.com/tutorials
  • http://www.easycfm.com/
  • http://www.carehart.org/ugtv/
like image 165
Henry Avatar answered Sep 19 '22 23:09

Henry


I can highly recommend Brian Kotek's "Form Utils" for cases such as this: http://www.briankotek.com/blog/index.cfm/2007/9/4/Implicit-Creation-of-Arrays-and-Structures-from-Form-Fields

I use this in every app I build, because working with arrays and structs on the form submission side is much more preferable to working with lists, imo.

like image 38
marc esher Avatar answered Sep 18 '22 23:09

marc esher