Cold Fusion Tips-N-Tricks
Fetching the contents of generated field names

Say you have an HTML form that contain x number of date fields. Say you want to use Cold Fusion's automatic validation of the date fields using the CFINPUT tag.

Catch is, you can not use the same name for more than one CFINPUT field like you can in a normal HTML input field.

First we want to build the form. The user has prompted in a previous page that they will be entering 10 dates (stored in num_dates).
 <CFFORM action="nextpage.cfm">
<CFLOOP index="1" to="#num_dates#">
<CFOUTPUT>
Date ###loop#:
<CFINPUT type="text"
name="date_#loop#"
validate="date"
required="Yes"
message="You must enter a valid date for date field number #loop#">
<br>
</CFOUTPUT>
</cfloop>
<CFOUTPUT>
<input type="Hidden" name="num_dates" value="#num_date#">
</CFOUTPUT>
</CFFORM>

On the next page, we need to get the contents of these variables named "date_1", "date_2", and so on to "date_10". If we used:
<CFSET curr_date=date_#loop#>
It would blow up.

If we used:
<CFSET curr_date="date_#loop#">
Curr_date would be set to "date_1", "date_2" and so on. Not what we wanted.

However, we can use the Evaluate function to grab the contents of the variable.
 <CFLOOP index="loop" from="1" to="#num_dates#">
<CFSET curr_date=Evaluate("date_#loop#")>
</CFLOOP>
Kinda think of Evaluate getting the CF server to parse out the contents and using it instead.

Return to the Cold Fusion Tips-N-Tricks topic list