0

Question: I have a HTML form with 60+ inputs that on submit I want to use ColdFusion to save all of the values into an array on the action page. Is there a more efficient way to save the values of all 60+ inputs without having to mention each one at a time?

I am looking for some sort of a loop or similar process that will save me time from having to write out all of the input names in my action page.

I can provide examples of my code if needed but they are just standard HTML form inputs (text and select) with a submit button at the bottom of the form.

10
  • If all form fields have the same name, you can simply do listToArray( form.fieldname ) Commented Jan 7, 2015 at 18:59
  • 2
    Why would you not use a normalized data model? If you don't know what that is, I've heard good things about the book, Database Design for Mere Mortals. Commented Jan 7, 2015 at 19:55
  • 1
    @Denoteone - Again, I would ask why? Storing data in a single column kind of defeats the purpose of a db. It will also make it extremely difficult to do anything useful with the data. If you intend to do any querying at all, you will want to store the data properly in separate, normalized tables. Commented Jan 7, 2015 at 20:43
  • 1
    I have to agree with @DanBracuk. Often the easy methods (I'll dump the whole object into one field) come back to bite you later. When all your data is stored together in one field, you almost end up with no other choice than selecting the whole field. Reliably parsing a string in SQL can get messy, and sometimes it's not possible. Also, selecting everything for some of it or parsing field for part of its value is both needless overhead. What if the client wants users whose names begin with b or who prefer red whizzbangs over blue wackymoles? Commented Jan 7, 2015 at 20:43
  • 1
    @cfqueryparam - I am not buying you a Coke :P I am keeping the caffeine for myself. Commented Jan 7, 2015 at 20:47

2 Answers 2

3

NOTE: Having seen your comments on your OP, that you want to mass insert into one db field. You should be careful with this. While it's easy now, it can make working with the data in queries very very difficult and marginally bit harder in Cold Fusion.


This should work

<cfset fArr = ArrayNew(1)>
<cfset fcount = 1>
<cfloop list="#form.fieldnames#" index="f">
  <cfif not listfind("bad,field,names",f)>
    <cfset fArr[fcount] = form[f]>
    <cfset fcount = fcount + 1>
  </cfif>
</cfloop>

The CFIF is so you can omit field names that you might like to. If you want to collect every one, just remove it. Perhaps you have a submit button named "gobtn" that you don't want to gather into the array., you would make the list gobtn like <cfif not listfind("gobtn",f)>

You can use a similar thing with any scope or struct, except that they don't typically have a fieldnames attribute or anything like it, but Cold Fusion has the function StructKeyList() which also works on the form scope but unfortunately StructKeyList(form) also includes the field fieldnames which can get annoying, it's easier just to use the built in variable.

Demonstrating with URL scope (functionally the same as a struct)

<cfset fArr = ArrayNew(1)>
<cfset fcount = 1>
<cfloop list="#StructKeyList(url)#" index="f">
  <cfif not listfind("bad,field,names",f)>
    <cfset fArr[fcount] = url[f]>
    <cfset fcount = fcount + 1>
  </cfif>
</cfloop>

(Chris Tierney's use of ArrayAppend is correct btw, you can do something like this which is more reasonable). I don't know why I didn't include that.

Also ArrayNew(1) and [] are the same, but earlier versions of CF don't support it, so I habitually answer with ArrayNew(1).

<cfset fArr = ArrayNew(1)>
<cfloop list="#StructKeyList(url)#" index="f">
  <cfif not listfind("bad,field,names",f)>
    <cfset ArrayAppend(fArr,url[f])>
  </cfif>
</cfloop>

Given some of your comments..

Another option is SerializeJSON

You can do this

<cfset formCopy = Duplicate(form)>
<!--- We have to duplicate the struct so that we can safely modify a copy without affecting the original --->
<cfset DeleteItems = "fieldnames,gobtn">
<cfloop list="#deleteItems#" index="df">
  <cfset StructDelete(formCopy,df)>
</cfloop>
<cfset ForDBInsert = SerializeJSON(formCopy)>
<!--- ForDBInsert now contains a JSON serialized copy of your data. You can insert it into
  the database as such, and call it back later. --->

Calling it back

<cfquery name="getFD">
  select FormDump from Table
</cfquery>

<cfoutput query="getFD">
  <cfset ReBuild = DeserializeJSON(FormDump)>
  <!--- You now have a struct, Rebuild, that contains all the fields in easy to access format --->
  <cfdump var="#ReBuild#">
</cfoutput>
Sign up to request clarification or add additional context in comments.

4 Comments

I was going to ask about form.fieldnames but didn't think any data was stored in it. I will try your recommendation above. Thanks for your help! +1
@Denoteone I added an alternate method that you might find interesting.
Thanks CFqueryparam your information on using JSON may be a better solution because of what I am going to be doing with the data. I am posting another question regarding this part of my project I will comment the URL if you want to take a look.
3
var fieldNameArray = listToArray(form.fieldNames);
var formArray = [];

for( fieldName in fieldNameArray ) {
    if( fieldNamme != "fieldNames" ) {
        arrayAppend( formArray, { name = fieldName, value = form[fieldName] } );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.