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

cfloop over dynamically generated check boxes and radio buttons

Explorer ,
Jan 26, 2012 Jan 26, 2012

Copy link to clipboard

Copied

Hello, I have a page where a user can select a project, via a checkbox then select a radio buttion with the following options "Support, Oppose, Not Known". The form fields are dynamically generated, can be anywhere from 1 to many. here is a screenshot:

project.jpg

A user has to at least select 1 but can select many. The checkbox names are dynamically generated and follow this naming convention:

f_project_1

f_project_2

the radio buttons are named similar:

f_support_1

f_support_2

So, on my submit page I need to loop through each of these and insert into my database. before I just had a select box with projects(no support, oppose stuff) and I used the following code and it worked great.

<CFLOOP INDEX="projectlist" LIST="#Form.f_project#">

          <cfoutput>

                    <CFQUERY  datasource="#datasource#" name="insertproject">

                              INSERT into contactrecord_project

                              (contactrecord_project_id,

                              contactrecord_project_nm,

            project_support)

                              Values ('#newid#','#projectlist#',)

                    </CFQUERY>

          </cfoutput>

</CFLOOP>

I'm just brain dead today, can someone please help me out with looping over the list and getting it inserted into my database

TOPICS
Advanced techniques

Views

3.5K

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 ,
Jan 26, 2012 Jan 26, 2012

Copy link to clipboard

Copied

<cfdump var="#form#">

<cfform>

Proj 1: <cfinput name="f_project_1" type="checkbox">||

Support 1: <cfinput name="f_support_1" type="radio" value="support11">|

Support 1: <cfinput name="f_support_1" type="radio" value="support12">|

Support 1: <cfinput name="f_support_1" type="radio" value="support13" checked="yes">|

<br>

Proj 2: <cfinput name="f_project_2" type="checkbox">||

Support 2: <cfinput name="f_support_2" type="radio" value="support21">|

Support 2: <cfinput name="f_support_2" type="radio" value="support22">|

Support 2: <cfinput name="f_support_2" type="radio" value="support23" checked="yes">|

<br>

Proj 3: <cfinput name="f_project_3" type="checkbox">||

Support 3: <cfinput name="f_support_3" type="radio" value="support31">|

Support 3: <cfinput name="f_support_3" type="radio" value="support32">|

Support 3: <cfinput name="f_support_3" type="radio" value="support33" checked="yes">|

<br>

<cfinput name="sbmt" type="submit" value="send">   

</cfform>

<cfloop from="1" to="6" index="idx">

    <cfset projectName = "f_project_" & idx>

   

    <!--- Do something (for example, an insert) only if a project is selected on the form. Otherwise, do nothing. --->

    <!--- Note: Checked radio buttons return value, however checked checkboxes only return "ON" --->

    <cfif structKeyExists(form,"#projectName#")>

        Your insert query goes here. The values to insert are: <br>

        <cfoutput>Project name: #projectName#; Support: #form["f_support_" & idx]#</cfoutput><br>

    </cfif>

</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
Explorer ,
Jan 27, 2012 Jan 27, 2012

Copy link to clipboard

Copied

Hey BKBK.....How can I get only the number into the projectname variable? Right now it is inserting "f_project_1" for example....I just want "1"

Thanks.

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 ,
Jan 27, 2012 Jan 27, 2012

Copy link to clipboard

Copied

BKBK's answer gave you the value of the formfield.  If you run this code:

<cfdump var = "#form.f_project_1#">

what do you see?

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
Explorer ,
Jan 27, 2012 Jan 27, 2012

Copy link to clipboard

Copied

Hi Dan, Here is my code for  the dynamic creation of the checkboxes & radio buttons

<cfloop query="getprojects">

                           <tr>

                     <td bgcolor="##CCCCCC"><input type="checkbox" name="f_project_#project_name_id#" value="#project_name_id#" id="f_project_#project_name_id#"  onClick="javascript:enableField_#project_name_id#()">#project_name#</td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="1" id="f_support_#project_name_id#" disabled></td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="0" id="f_support_#project_name_id#" disabled></td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="2" id="f_support_#project_name_id#" checked disabled></td>

                  </tr>

          </td></tr>

          </cfloop>

So If I dump the form variable f_project_1 it = 1

BKBK's code does not appear to me to use the value created from the form(I did not provide that code when I first created this thread)....he sets it with   <cfset projectName = "f_project_" & idx>

Thanks for any assitance you can provide.

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 ,
Jan 28, 2012 Jan 28, 2012

Copy link to clipboard

Copied

pmlink360 wrote:

Hey BKBK.....How can I get only the number into the projectname variable? Right now it is inserting "f_project_1" for example....I just want "1"

pmlink360 wrote:

Hi Dan, Here is my code for  the dynamic creation of the checkboxes & radio buttons

<cfloop query="getprojects">

                           <tr>

                     <td bgcolor="##CCCCCC"><input type="checkbox" name="f_project_#project_name_id#" value="#project_name_id#" id="f_project_#project_name_id#"  onClick="javascript:enableField_#project_name_id#()">#project_name#</ td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="1" id="f_support_#project_name_id#" disabled></td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="0" id="f_support_#project_name_id#" disabled></td>

                   <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="2" id="f_support_#project_name_id#" checked disabled></td>

                  </tr>

          </td></tr>

          </cfloop>

So If I dump the form variable f_project_1 it = 1

BKBK's code does not appear to me to use the value created from the form(I did not provide that code when I first created this thread)....he sets it with   <cfset projectName = "f_project_" & idx>


I assumed the project names to be f_project_1, f_project_2, and so on. That is what your "naming convention" said.

In any case, your code appears to have done most of the work already. My suggestion should now read:

<cfloop from="1" to="6" index="idx">

    <cfset projectName = "f_project_" & idx>

   

    <!--- Do something (for example, an insert) only if a project is selected on the form. Otherwise, do nothing. --->

    <cfif structKeyExists(form,"#projectName#")>

        Your insert query goes here. The values to insert are: <br>

        <cfoutput>

          Project ID: #idx#;

         Support: #form["f_support_" & idx]#

         </cfoutput><br>

    </cfif>

</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
Explorer ,
Jan 28, 2012 Jan 28, 2012

Copy link to clipboard

Copied

Thanks BKBK...that did it....I appreciate you getting back to me.

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 ,
Jan 30, 2012 Jan 30, 2012

Copy link to clipboard

Copied

LATEST

OK

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