Link to home
Start Free TrialLog in
Avatar of day6
day6Flag for United States of America

asked on

Dynamic Variable Syntax

I'm trying to process a form submission where there are 3 fields with different names, but 10 instances of the fields. I know it sends the duplicate fields as a delimited list.

My issue is that I'm using the CFLOOP to create 3 variables each iteration, each with a concatenated index value. So my output from the cfloop gives me these variables.
CFLOOP 1
<cfset var1=1>
<cfset var2=6>
<cfset var3=15>

CFLOOP 2
<cfset newvar1=16>
<cfset newvar2=89>
<cfset newvar3=82>

CFLOOP 3
<cfset anothervar1=7>
<cfset anothervar2=9>
<cfset anothervar3=11>

So I have 3 different cfloops doing the same thing. Now, I am ready to insert these variables into a database using the same CFLOOP idea

<cfloop index="x" from="1" to="10">
<cfquery name="addreco" datasource="#mydsn#">
INSERT INTO myDatabase
VALUES (foo,foobar,foofoo)
('#Var("#x#")#','#newvar("#x#")#','#anothervar("#x#")')
</cfquery>

</cfloop>

Is the data inserting formatted right since I'm technically combining two variable names to create a single variable name?  I've read multiple examples online for ways to do this and have only been confused by the proper syntax. I have not yet tested this theoretical format to make the data work.

Help
Avatar of gdemaria
gdemaria
Flag of United States of America image

As an example, to access this variable...

<cfset newvar1=16>

Use this format..

#variables["newVar" & X]#

This is structure format, using the variables scope (which is the default scope since you didn't specify one)
Avatar of day6

ASKER

I have a checkbox field on the submitting form page that is named the same for all the checkboxes on the page in order to facilitate a Javascript function that I need. When I submit the form, I will not know in advance what the name of that checkbox field will be... so I'm having a hard time creating a cfloop list when I don't know how to name the checkbox field in the "list" variable in the CFLOOP parameter.

<cfloop list="#unknownFormFieldName#&aa"  

The field is dynamically named "1aa" with the 1 being the variable, but the aa is constant. So if the user submits ONE particular form on the page (multiple forms are present) the action page will see the dynamically named checkbox field, but I only know that the field is some number with aa following it in the name.

How do I define my LIST value when I don't know the field name for sure?
Not sure I understand what you're doing.

But hope this helps.   If you want a list of fields submitted by your form, you can do this..

 <cfset formFieldList = structKeyList(form)>

If you know the name of a checkbox field and it may be unchecked, you can do this...

<cfparam name="variables,myCheckBoxName#X#" default="">

where X is the numbers, 1, 2, 3..

I would think you would know all the possible fields on your page, even if you don't know which ones are passed.

If that is true, loop through all possible fields and CFPARAM them to ensure they exist...

<cfloop index="aField" list="name,address,phone,....all possible fields">
    <cfparam name="form.#aField#" default="">
</cfloop>

Now all will exist and you can check them to see if they are populated
Avatar of phox6801
phox6801

I don't know the name of the checkbox field since it is generated dynamically.

My script uses a random number and then aa immediately after.

Form.randomnumberaa

this is the issue
So when you write your form field, you could also place the name of the form field into a hidden input field.

<cfset thisFieldName = randomNumber & "aa">
<input type="text" name="#thisFieldName#".....>
<input type="hidden" name="AllMyFields" value="#thisFieldName#">

You can do this repeatedly using the same hidden form field.   After posting the FORM.AllMyFields value would be a comma delimited list of all your hidden fields.


<cfset thisFieldName = randomNumber & "XX">
<input type="text" name="#thisFieldName#".....>
<input type="hidden" name="AllMyFields" value="#thisFieldName#">

<cfset thisFieldName = randomNumber & "YY">
<input type="text" name="#thisFieldName#".....>
<input type="hidden" name="AllMyFields" value="#thisFieldName#">


<cfset thisFieldName = randomNumber & "ZZ">
<input type="text" name="#thisFieldName#".....>
<input type="hidden" name="AllMyFields" value="#thisFieldName#">

<cfoutput>#form.allMyFields#</cfoutput>

     87XX,45YY,23ZZ
I don't know the name of the checkbox field since it is generated dynamically.

My script uses a random number and then aa immediately after.

I don't want to interrupt the flow, but ... are the numbers truly random (15,86,92,43,....,546) or just sequential (1,2,3,4....10)? EDIT If they're sequential, gdemaria's earlier suggestion of using structure notation should work fine. Something like this.  I'm not near CF to test the syntax, so beware typos...

    <cfloop index="x" from="1" to="10">
          <cfset theFieldValue = FORM[x &"aa"]>
          .. do something with #theFieldValue# ....
    </cfloop>
Avatar of day6

ASKER

@gdemaria

I can now get the form field name using your method, but not sure how to then put that in the proper syntax for the cfloop LIST section

<cfloop list="#form.hiddenfieldValue#"...

Is that correct?
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial