• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Variable within a Variable

Contributor ,
Dec 09, 2018 Dec 09, 2018

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

Views

385

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Dec 09, 2018 Dec 09, 2018

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=

...

Votes

Translate

Translate
Contributor ,
Dec 09, 2018 Dec 09, 2018

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2018 Dec 09, 2018

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#']#

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2018 Dec 09, 2018

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 10, 2018 Dec 10, 2018

Copy link to clipboard

Copied

LATEST

ghanna1  wrote

I'm sure it's something valuable!

Gary

Oh, it only prevents things like SQL-injection attacks, and such. 

V/r,

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation