Reputation: 1357
Summary:
the problem is when i add the "_#i#" to the end of the field names!
Detail:
I have a variable amount of form fields that need to be inserted in to the db at the same time, different rows.
so I looped the form and incremented the name of the fields.
so I have title_1, title_2, title_3 etc...
next I wanted to insert them in the database (one submit button) by looping the insert by the amount of form fields.
I'm using ColdFusion Server Standard 8,0,1,195765
The Error: Invalid CFML construct
I've tried all sorts - any suggestions would be much appreciated.
<cfoutput>
<cfloop from="1" to ="#VARIABLES.fieldTotal#" index="i">
<cfset VARIABLES.insert = theObj.the_insert(
the_id = FORM.the_id_#i#
, title = FORM.title_#i#
, author = FORM.author_#i#
, caption = FORM.caption_#i#
)>
</cfloop>
</cfoutput>
Upvotes: 1
Views: 575
Reputation: 7833
The correct answer is already given by Sam Farmer, but just to clarify how accessing struct members works:
FORM.var
is equal to FORM["var"]
Access using a dot will not get evaluated though. Whenever you want to dynamically access members in structs, you need to use square brackets.
FORM["the_id_#i#"]
is equal to FORM["the_id_" & i]
.
And remember to sanitize user inputs (especially GET/URL and POST/FORM data):
<cfloop from="1" to="#VARIABLES.fieldTotal#" index="i">
<cfif structKeyExists(FORM, "the_id_" & i) and reFind("^[0-9]+$", FORM["the_id_" & i])
and structKeyExists(FORM, "title_" & i)
and structKeyExists(FORM, "author_" & i)
and structKeyExists(FORM, "caption_" & i)>
<cfset VARIABLES.insert = theObj.the_insert(
the_id = FORM["the_id_" & i],
title = FORM["title_" & i],
author = FORM["author_" & i],
caption = FORM["caption_" & i]
)>
</cfif>
</cfloop>
structKeyExists(FORM, "the_id_" & i)
is equal to isDefined("FORM.the_id_#i#")
.
Upvotes: 3
Reputation: 4118
form[ "the_id_#i#" ]
All ColdFusion scopes are structures and can be accessed this way.
Upvotes: 12