• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Dynamic form input name

New Here ,
Dec 22, 2014 Dec 22, 2014

Copy link to clipboard

Copied

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.

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 23, 2014 Dec 23, 2014

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 />

...

Votes

Translate

Translate
LEGEND ,
Dec 22, 2014 Dec 22, 2014

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 23, 2014 Dec 23, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 23, 2014 Dec 23, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2014 Dec 23, 2014

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2014 Dec 26, 2014

Copy link to clipboard

Copied

LATEST

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">.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation