Skip to main content
BreakawayPaul
Inspiring
September 23, 2011
Question

I need help fleshing out an idea for a form

  • September 23, 2011
  • 2 replies
  • 840 views

I have a form I build for my agency that adds records to a database.  It's sort of an online survey that's used by different offices within my agency, and it's "recycled" every year or so.

It consists of some info fields for the user filling it out (name, organization, etc) that are always there, but it also has some textarea fields that change whenever it's "recycled".

It's a large form with a lot of serverside error checking, and it's a bit tedious to recode the HTML (not to mention the target page) everytime the form gets recycled, so I had an idea.

My idea was to create a table in the database for the form, and have the following columns:

field_type (i.e. text, textarea,  etc)

field_name (for the name="" of the input tag

field_label (for the <label> tag)

field_size

field_order (so I can control the order that the fields show up)

Then use a query to have my CF page dynamically build the form.

While I'm pretty sure the above would work, I'm not so sure how to code the target page, since the FORM.names won't remain the same.  I thought about tying to capture all the form fields in some sort of array, but I'm not really sure how to do that.  Any ideas?

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    September 24, 2011

    There is one answer. However, it makes the form even longer!

    Consider that the columns you've mentioned represent attributes of each form field. Then include hidden form fields to hold the values of these attributes. For example, for the text field named abracadabra, you will have something like

    <input name="abracadabra" type="text">

    <input name="abracadabra_type" type="hidden" value="text">

    <input name="abracadabra_label" type="hidden" value="mylabel">

    <input name="abracadabra_size" type="hidden" value="10">

    <input name="abracadabra_order" type="hidden" value="3">

    When the form is submitted, the values in the action page will be

    form.abracadabra

    form.abracadabra_type

    form.abracadabra_label

    form.abracadabra_size

    form.abracadabra_order

    BreakawayPaul
    Inspiring
    September 26, 2011

    Ok, finally back to work after a very busy weekend, so I can try playing with this some.

    So as Dan says, I can capture the form fields pretty easily, so I'll try to build that out today.  But I think the hard part will be knowing what the form field names will be for the DB insert and update.  I might still have to re-code that part for each iteration,

    BKBK
    Community Expert
    Community Expert
    September 26, 2011

    BreakawayPaul wrote:

    So as Dan says, I can capture the form fields pretty easily, so I'll try to build that out today.  But I think the hard part will be knowing what the form field names will be for the DB insert and update.  I might still have to re-code that part for each iteration,

    As far as I know, there is no way of obtaining field type, label, size etc. from a submitted form, without having submitted those values themselves as form fields.

    Inspiring
    September 23, 2011

    Regarding capturing form fields without knowing their names, there are at least two ways.  You can loop through the form collection or you can loop through the formfields list.  Then you use array notation to get the value.