Click to See Complete Forum and Search --> : Arrays in a Form


SnipeHunter
12-03-2009, 02:40 PM
How do I get a CF Form to pass an array variable?

For this example script I want the data to list at the top and pass back to prefill the fields as well if you hit submit. This passes the basic variable just fine, but not the array. So what is the proper method for passing an array through a form?

<!---one string variable, one array --->
<cfif Not IsDefined ("Form.nameF")>
<cfset Form.nameF="">
</cfif>

<cfif Not IsDefined ("Form.address")>
<cfset form.address=ArrayNew(1)>
<cfset form.address[1]="">
<cfset form.address[2]="">
<cfset form.address[3]="">
</cfif>

<!---form to take in new variable and array values--->
<cfoutput>Previous Entry<br>
Name:#nameF#<br>
<cfloop from="1" to="3" index="i">
Address #i#: #address[i]#<br>
</cfloop>
</cfoutput><br>
<cfform name="basic" method="post">
First Name: <cfinput name="nameF" value="#Form.nameF#" type="text" size="30"><br />

<cfloop from="1" to="3" index="i">
Address <cfoutput>#i#: </cfoutput><cfinput name="address[i]" value="#form.address[i]#" type="text" size="30"><br />
</cfloop>

<cfinput name="submit" type="submit" value="Submit">
</cfform>

TiGGi
12-07-2009, 03:17 PM
Array is an complex object where form field is simple value so you can not pass array through form, only form fields. What you can do i scalar Array into forms fileds, which means grab whatever is in array and create form fields from it just like your example does with address. One that form is sibmited it will pass that info as form fields where you can do whatever with data even recreate an array again.