Working with Form Arrays in ColdFusion?

2020-04-02 18:27发布

问题:

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!

回答1:

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/


回答2:

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.



回答3:

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.



回答4:

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


回答5:

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.



回答6:

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



回答7:

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.