Ask a Question related to Coldfusion - Getting Started, Design and Development.

  1. #1

    Default Getting form fields

    Hi all

    I am coming from a different language to CF so please forgive if this is
    obvious to some of you.

    I need to insert data into a SQL DB and I dnot want to write the name of the
    field and the values, but would like to have the code be generated dynamically.

    So in the "other" language I could do something like:
    >formvariables name="myform" contains="f_">
    #nameoffield# #valueoffield#
    </formvariables>

    This would give me the fields that are named with "f_field1", "f_field2".

    Now trying to do the same with CF. So I started with:

    <cfloop index="LoopCount" list="#form.fieldnames#" delimiters=",">
    <cfif #LoopCount# CONTAINS "f_">
    #LoopCount# #form["#LoopCount#"]#
    </cfif>
    </cfloop>

    Success, I got the name and field values too.

    Ok, now I am trying to insert this into a SQl statement:

    insert into mydb
    (<cfloop index="LoopCount" list="#form.fieldnames#" delimiters=","><cfif
    #LoopCount# CONTAINS "f_">#LoopCount#,</cfif></cfloop>)
    values(<cfloop index="LoopCount" list="#form.fieldnames#" delimiters=","><cfif
    #LoopCount# CONTAINS "f_">#form["#LoopCount#"]#,</cfif></cfloop>)

    So this generates the SQL code dynamically. The only problem I have so far is
    that I dont know how to get rid of the last "," in both loops. Also is this a
    "recommended" way of dealing with form data?

    Also, one more thing, is there a list of what is available inside a #form#
    tag? I looked all over Macromedia documentation and found nothing.

    TIA.

    nitai_co Guest

  2. Similar Questions and Discussions

    1. Dynamically Adding Form Fields to CF Flash Form
      I think I know the answer to this but, after some failed Googles, I wanted to double-check here. I have a Flash-based event registration form I'm...
    2. add form fields ?
      We've recently upgraded from Acrobat 5 to 7 Standard. I cannot seem to locate the Form field button anymore so that I can add fields to the pdf file....
    3. Populate form values based on previous same form fields
      This message is cross posted in alt.comp.lang.php & comp.lang.javascript I have a form for a user to input an establishment's hours and what time...
    4. Fields don't appear on form
      Alright - I'm know this has to be something complete simple - but I use the form design wizard to build a form using a query - when I get out of...
    5. Fields on Form Help
      Would have to do so programmatically
  3. #2

    Default Re: Getting form fields

    I typically do not process entire forms this way. The reason being that it
    gives you no control over the data being inserted. If a field was added to the
    form, but not to the database table, the query would fail. Also, If the data
    being inserted was of mixed types, it would also be difficult to format
    correctly .. and to use cfqueryparam (as is recommended) because it requires a
    datatype. That's just my two cents ;-)

    You can use CFDUMP on your action page to display the contents of the form
    structure.

    <cfdump var="#form#">

    mxstu Guest

  4. #3

    Default Re: Getting form fields

    Thank you for your answer.

    I am just wondering why one would like to type a lot of SQL code when there
    are about 40 or more fields in the DB while coding.

    Of course, when you want to code the <cfqueryparam> with each insert/update
    then one has to do it this way.

    Another language, another way of coding, I guess :-)



    nitai_co Guest

  5. #4

    Default Re: Getting form fields

    Still, ist there a way to get rid of the last "," iny my original post?

    TIA.
    nitai_co Guest

  6. #5

    Default Re: Getting form fields

    There are a few ways to do it. Here is one method ...

    Also, the posted code contain a lot of extra pound signs where they are not
    needed. You could actually change this statement

    <cfif #LoopCount# CONTAINS "f_">
    #LoopCount# #form["#LoopCount#"]#
    </cfif>

    to

    <cfif LoopCount CONTAINS "f_">
    #LoopCount# #form[LoopCount]#
    </cfif>



    <!--- I believe you're just looking for fields that start with "f_" --->
    <!--- so I replaced the CONTAINS check with a substring EQ check --->
    <cfloop from="1" to="#ListLen(form.fieldnames)#" index="i">
    <cfset currField = ListGetAt(form.fieldNames, i)>
    <cfif Left(currField, 2) eq "f_">
    #currField#
    <cfif i neq ListLen(form.fieldNames)>,</cfif>
    </cfif>
    </cfloop>

    mxstu Guest

  7. #6

    Default Re: Getting form fields

    I am just wondering why one would like to type a lot of SQL code when there are
    about 40 or more fields in the DB while coding.

    In many cases there is no way around it. I've rarely inserted into a table
    with more than 20 columns, where all of the columns had the same data types.
    Imagine a dynamic insert where some of the values are varchars and must have
    single quotes while others are numeric and do not need single quotes, and
    others are dates that must be handled correctly with CreateODBCDate()
    functions, etc... it can get a bit kludgey. IMO it's not worth it ;-)


    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139