8

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!

Dave DuPlantis
  • 6,378
  • 3
  • 26
  • 30
Jakub
  • 20,418
  • 8
  • 65
  • 92

7 Answers7

26

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 ;)

Henry
  • 32,689
  • 19
  • 120
  • 221
  • 7
    FWIW, PHP interprets form elements named with a "[]" as an array. CF treats form elements of the same name as a comma-delimited list. – Adrian J. Moreno May 27 '10 at 19:33
  • 1
    What to do if your input contains a comma?! Is there a way to escape it? – rkingon Dec 05 '12 at 21:09
  • @rkingon not sure what you're asking. Pls ask a new question. – Henry Dec 05 '12 at 22:52
  • 1
    The list from the form array input (ie: ITEMS[]) comes back comma delimited.. So, if your input happens to contain a comma, CF reads it as a delimitation. I ended up just appending _1 _2 _3 on my form field names with js and having a hidden input with the total rows then looping through in coldfusion – rkingon Dec 07 '12 at 18:19
  • if you use CF10, u can set it up in a way that multiple values are returned as an array. – Henry Dec 07 '12 at 23:00
  • 1
    @Henry I believe the option you're referring to is `this.sameformfieldsasarray=true;` however this is ultimately flawed. [Source](https://www.raymondcamden.com/2014/02/25/ColdFusion-and-Form-Fields-with-the-Same-Name/) – MPaul Aug 04 '16 at 19:31
  • **Values from checkboxes are just comma separated values, which is a List in ColdFusion** - Expanding on Henry's answer, if the page handling the FORM data loops through an array of FORM fields with the same name name, the error message that ColdFusion logs if trying to access an array position (due to a comma in the original field's value) is _The element at position **X** cannot be found_ – MPaul Aug 04 '16 at 19:41
  • Coldfusion ignores empty fields. – csandreas1 May 28 '21 at 06:46
4

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.

marc esher
  • 4,871
  • 3
  • 36
  • 51
  • Form Utils is incredibly valuable and is my preferred way of handling form arrays in CFML also. Here's a link to an updated version of Form Utils designed to work with newer versions of ColdFusion: https://github.com/jmohler1970/FormUtils – Dave L Jul 26 '19 at 16:37
2

With you list that are Id's it works fine, but if you have an array with comma's in then you're stuck.

In that case you can use the Java method getParameterValues.

<cfdump var="#getPageContext().getRequest().getParameterValues('ITEMS')#">

This will give you an standard CF array which you can use.

MrSoolmaan
  • 237
  • 1
  • 11
  • Note that this doesn't work with multipart forms. In such a case Java has a method called `getPartsArray()` http://cfsearching.blogspot.ca/2010/02/form-field-values-multipart-forms-and.html – MPaul Aug 04 '16 at 20:17
2

See also the second answer here. It describes how to retrieve values from a field with multiple instances on a form as an array. I have to say though, I've been working in CFML for many years, and I've yet to do this myself, or see it done in any app I've worked on. I think that's just because avoiding commas is very much simpler, but if you can't or don't want to work around it that way, it is possible.

Community
  • 1
  • 1
enigment
  • 3,316
  • 7
  • 30
  • 35
2

Also, note that in an ajax world, if you json encode the entire body of a post request, rather than individual form fields, it can be any arbitrary data structure, retrievable easily on the server. The snippet below shows how to get to it from ColdFusion. I'm not certain about other languages, but it's almost certainly possible.

To send a post like that using jQuery, JSON.stringify your data before passing it to jQuery, as noted here and here.

If you're building your own ajax request, the punchline would be:

xhr.send(JSON.stringify(data));

To access that data on the server side, this ColdFusion example looks first for that kind json-encoded post body, then a post with json data in the form field 'input', then in a url field with that same name. In all cases, the resulting data gets deserialized and assigned to the local var 'input', which you can then put in request scope, 'rc', or whatever your code expects.

if (Find('application/json', cgi.content_type))
{
    input = ToString(GetHttpRequestData().content);
    if (IsJSON(input))
        input = DeserializeJSON(input);
}
else if (StructKeyExists(form, 'input') and IsJSON(form.input))
    input = DeserializeJSON(form.input);
else if (StructKeyExists(url, 'input') and IsJSON(url.input))
    input = DeserializeJSON(url.input);
Community
  • 1
  • 1
enigment
  • 3,316
  • 7
  • 30
  • 35
0

For ColdFusion 10+, if you use the sameformfieldsasarray setting in your Application.cfc like this:

component {
    this.name = "testingzone2c";
    this.sameformfieldsasarray=true;
}

You will get an actual array of form fields with the same name.

ColdFusion 10 Missing Feature - Form Fields and Arrays

Community
  • 1
  • 1
Henry
  • 32,689
  • 19
  • 120
  • 221
0

I suggest that you remove the "[]" from the name since it disallows dot notation as mentioned in another answer.. When more than one form element contains the same name attribute, the browser will concatenate all of the values into a comma delimited string when submitting the form. Fortunately, ColdFusion has many functions which treat a delimited string as a list. You can use <cfloop> along with these functions to consume the list.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268