Skip to main content
Known Participant
February 16, 2010
Question

Form Name with a Variable

  • February 16, 2010
  • 3 replies
  • 385 views

I have a form that I built with a cfloop and I want to be able to name the field names

like this field_#ID# as it does the loop.  Is there a way to do this?

This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
February 17, 2010

The approach for the form tag is slightly different from that of cfform. Put the following code in a page, open it and view the page source in the browser.

<form>

<cfloop from = "1" to = "5" index="idx">

    <cfoutput><input name="myTxtBox_#idx#" type="text"></cfoutput><br>

</cfloop>

</form>

<cfform>
<cfloop from = "1" to = "5" index="idx">

<!--- cfoutput unnecessary --->
    <cfinput name="myFormField_#idx#" type="text"><br>
</cfloop>
</cfform>

Inspiring
February 16, 2010

Ian's answer pertained to processing the form.  To create it, assuming you were looping through a query, change the cfloop tag to a cfoutput tag.

ilssac
Inspiring
February 16, 2010

You are probably looking for array syntax.

<cfoutput>#form["field_" & id]#</cfoutput>