Personal tools
Home Webmaster Resources ColdFusion Using Dynamic Object Names

Using Dynamic Object Names

There may come a time when you need to have a varying number of HTML objects on your page.  An often cited example is when you have a form, say with multiple check boxes.  The number of checkboxes depends on a certain value, for instance:

<cfset num_boxes = url.num_boxes>

<form action="next.cfm" method="get">
    <input type="hidden" name="num_boxes" value="#num_boxes#">
    <cfloop index="i" from="1" to="#num_boxes#">
   
       <cfoutput><input type="checkbox" name="box#i#" /></cfoutput>

    </cfloop>
    <input type="submit" />
</form>



The question is, now, how to I obtain the values of those checkboxes once the form is submitted.  This is where the ColdFusion Evaluate function comes in handy.  The following script is the action page submitted to from the form above.

<cfloop index="i" from="1" to="#url.num_boxes#">

    <cfoutput>#Evaluate("url.box" & i)#</cfoutput>

</cfloop>

Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.