Skip to main content
Inspiring
Dec 9, 2018 1:24 PM UTC
Answered

Variable within a Variable

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

    This topic is closed to new replies. Start a new post to keep the conversation going.
    ghanna1Correct answer
    Inspiring
    Dec 9, 2018 2:58 PM UTC

    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>

    4 replies

    ghanna1AuthorCorrect answer
    Inspiring
    Dec 9, 2018 2:58 PM UTC

    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>

    Translate
    BKBK
    Community Expert
    Community Expert
    Dec 9, 2018 3:30 PM UTC

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

    Translate
    ghanna1Author
    Inspiring
    Dec 9, 2018 3:58 PM UTC

    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

    Translate