7 Replies Latest reply on Jan 30, 2012 5:06 AM by BKBK

    cfloop over dynamically generated check boxes and radio buttons

    pmlink360

      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

        • 1. Re: cfloop over dynamically generated check boxes and radio buttons
          BKBK Adobe Community Professional & MVP

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

          • 2. Re: cfloop over dynamically generated check boxes and radio buttons
            pmlink360 Level 1

            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.

            • 3. Re: cfloop over dynamically generated check boxes and radio buttons
              Dan Bracuk Level 5

              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?

              • 4. Re: cfloop over dynamically generated check boxes and radio buttons
                pmlink360 Level 1

                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.

                • 5. Re: cfloop over dynamically generated check boxes and radio buttons
                  BKBK Adobe Community Professional & MVP

                  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>

                  • 6. Re: cfloop over dynamically generated check boxes and radio buttons
                    pmlink360 Level 1

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