Skip to main content
Known Participant
December 22, 2014
Answered

Dynamic form input name

  • December 22, 2014
  • 2 replies
  • 2787 views

Hi,

My brain is frozen or something because I couldn't think out this issue.  Basically my form input radio name and value are dynamically pulled from a database.  The name of the form is concat with a name and id is from a database.  On the form processing side, how would I get all the dynamic input radio name.  The value of the input will be attach to the name.  Hope that make sense. 

form.cfm

<cfquery name="get_form_name" datasource="#ds#">

     SELECT id, name

     FROM records

</cfquery>

<cfoutput>

<form action="form_process.cfm">

<cfloop query="get_form_name">

<p>name: <input type="radio" name="test_#get_form_name.id#" value="#get_form_name.name#" /></p>

</cfloop>

<input type="submit" value="submit">

</form>

</cfoutput>

form_process.cfm

somehow get all the input name from the FORM to set cfparam and value.  Once I have this, i have the form values that are passing over.

    This topic has been closed for replies.
    Correct answer WolfShade

    Like Carl Von Stetten, I'm a bit confused as to why you would want/need to do so.  I'm getting the feeling that you might be confused about a particular aspect of development, and might be incorporating something that seems like a good idea at this time, but may return to bite you in the undercarriage in the future.

    However, what you ask can be done using bracket notation.  (Untested.. I think this will work.)

    <cfloop index="i" list="#Form.FieldNames#" delimiters=",">
     
    <cfset form["#i#"] = form />

             <!--- OR --->

      <cfparam name="form['#i#']" default="#form#" />
    </cfloop>

    2 replies

    BKBK
    Community Expert
    Community Expert
    December 26, 2014

    Your requirement is that you want the application to remember the name of a query variable across multiple page requests. That is a typical case where you would use the session scope.

    Following your example,

    form.cfm

    <cfquery name="get_form_name" datasource="#ds#">

         SELECT id, name

         FROM records

    </cfquery>

    <cfset session.formRecords = structNew()>

    <form action="form_process.cfm" method="post">

    <cfoutput query="get_form_name">

    <p>name: <input type="radio" name="test_#id#" value="#name#" /></p>

    <cfset session.formRecords["test_#id#"] = name>

    </cfoutput>

    <input type="submit" name="sbmt" value="submit">

    </form>

    form_process.cfm

    <!--- The names and values of the form fields are stored, respectively, as key-value pairs in the structure session.formRecords--->

    <cfdump var="#session.formRecords#">

    Remarks

    1) I simplified the code somewhat. I also added post method to the form, as I assume that is what you are aiming for. Otherwise the form fields will be submitted via the URL.

    2) It seems to me the database table holds rows having distinct values of ID . Therefore, my guess is that the functionality you should be going for is <input type="checkbox"> instead of <input type="radio">.

    WolfShade
    Legend
    December 22, 2014

    You can loop through the fieldnames like so to get the field name and the associated value.

    <cfloop index="i" list="#Form.FieldNames#" delimiters=",">
      #i# = #Form#
    <br>
    </cfloop>

    Known Participant
    December 23, 2014

    Thanks for your help!  With this way, how would I define default form name like <cfset form.name = ""> or <cfparam name="form.name" default=""> since it's dynamic on the processing page.

    Carl Von Stetten
    Legend
    December 23, 2014

    If the inputs are generated dynamically, why do you need the <cfparam>?  If you wrap your form data processing logic in the loop that WolfShade provided, then much of the need for <cfparam> (at least as far as providing defaults) is alleviated.  Are you trying to copy the form fields/values into another scope?

    -Carl V.