Copy link to clipboard
Copied
I have a form that allows the user to create 5 different sets of results on the same form. I do this by using CFLoop. For example:
<cfloop from=”1” to=“5” Index=“i”>
Form FieldName#i#
So I end up with:
FieldName1
FieldName2
FieldName3
FieldName4
FieldName5
That part works great and keeps the form code short and clean considering there are 16 form fields.
Is there a way to loop over that data to insert it into a table. Each Index needs to be its own row in the table. For example…
<cfloop from=”1” to=“5” Index=“z”>
<cfquery datasource = “abc”>
Insert into XYZ (Name)
Values (#form.FieldName[z]#)
</cfquery>
What I don’t know how to do is use a variable (the index) within a variable (Form.Result). Parts of the form need to go into 6 different tables times 5 sections of the form would be 30 SQL statements vs. 6 if I can loop.
Thanks in advance for any assistance.
Gary
I found my answer....
#form["name#z#"]#
Basic Example:
<cfif isdefined('form.submit')>
<cfloop from="1" to="5" index="z">
<cfoutput>#i# = #form["name#z#"]# </cfoutput><br></cfloop></cfif>
<form method="post" action="">
<input type="text" name="name1" id="name1"><br>
<input type="text" name="name2" id="name2"><br>
<input type="text" name="name3" id="name3"><br>
<input type="text" name="name4" id="name4"><br>
<input type="text" name="name5" id="name5"><br>
<input type="submit" name="submit" id="submit" value=
...Copy link to clipboard
Copied
I found my answer....
#form["name#z#"]#
Basic Example:
<cfif isdefined('form.submit')>
<cfloop from="1" to="5" index="z">
<cfoutput>#i# = #form["name#z#"]# </cfoutput><br></cfloop></cfif>
<form method="post" action="">
<input type="text" name="name1" id="name1"><br>
<input type="text" name="name2" id="name2"><br>
<input type="text" name="name3" id="name3"><br>
<input type="text" name="name4" id="name4"><br>
<input type="text" name="name5" id="name5"><br>
<input type="submit" name="submit" id="submit" value="Submit"> </form>
Copy link to clipboard
Copied
I am glad to see that you found the solution. Nevertheless, remember to insert the form values safely. Use, for example
<
CFQUERYPARAM
VALUE=
"#form['name#z#']#"
CFSQLType=
"CF_SQL_INTEGER"
>
instead of just
#form['name#z#']#
Copy link to clipboard
Copied
Thank you for the reply and helpful suggestion. Off to my trusty friend google to figure out what cfqueryparam and cfsqltype do. I'm sure it's something valuable!
Gary
Copy link to clipboard
Copied
ghanna1 wrote
I'm sure it's something valuable!
Gary
Oh, it only prevents things like SQL-injection attacks, and such.
V/r,
^ _ ^