Dynamically create form elements and values

I am building a trouble ticket system which has many form elements that shrink or grow depending on question responses. I am wondering if there is a method to create/add the form elements on the fly from a database.

Example database columns:
label = “Order Number” or “Order System”
element = “cfinput” or “cfselect”
type = “text” or “”
name = “ordernumber” or “ordersystem”
values = “” or “opt1, opt2, opt3”

Method would build elements

<label for="ordernumber">
<cfinput type="text" name="ordernumber" values="" />
<label for="ordersystem">Order System</label>
<cfselect name= "ordersystem">
 <option value="opt1">opt1</option>
 <option value="opt2">opt2</option>
 <option value="opt3">opt3</option>

I assume I could pass an id# to a cfc to build the HTML and bind to a div, but need a little guidance or a tutorial.

Here’s my 2p :slight_smile:

Forms are down to the view to control. Not your database or model and I think this solution is going to get messy when you have to start dealing with different exception for your “dynamic fields”.

If the forms have to change based on the user’s decisions then I would say two things would handle this better:

a) Separate pages / forms based on the user’s general decisions.

b) Use Javascript to enable and disable, hide or unhide options on the form. That way you’re controlling them on the front end based on their specific decisions.

In terms of processing / validating the submission in ColdFusion I would just handle thing as if the whole form were being sent. Handle empty fields etc. appropriately.



Thanks James,

option a is what we have now and its over 120 forms to manage.
option b is not a likely option. we will have 60+ form items.

I think I am just going to store each element’s html rather than trying to build the element on the fly and just add the ones I need based on answer sets. That way we can get some reuse. My only real concern is preserving the single and double quotes so I may have to do some character replacement to make it work.