8 Replies Latest reply on Dec 1, 2009 2:02 PM by Smdsports.com

    Quetion regarding checkboxes and inputting information

    Smdsports.com Level 1

      I am runing a form that pulls the checkboxes from a query.  They have the same name, but different values.

       

      On the action page, for each checkbox checked, a new record must be created in the db.

       

      Im' sure this has to do with counting and setting a loop number, but for some reason I am drawing a blank.

       

      The query is basic....

       

      <cfquery name="whatever" datasource=#dbname#>

           insert into ach_world_done

                (id,scoutid,den,world)

           Values

                ('#url.id#','#url.scoutid#','#url.den#','#form.world#')

      </cfquery>

       

      As it is written now, it conmas the checkboxes on the previous page, so if there are two boxes checked, it does info1,info2 and puts this into the db.  I need to break this up.

       

       

       

      any help would be greatly appreciated.

       

      Sharon

      www.smdscouts.com

        • 1. Re: Quetion regarding checkboxes and inputting information
          Dan Bracuk Level 5

          If your form has checkboxes, and they all have the same name, on your action page, you will get one of two things.  If you boxes were checked, the variable won't exist.  Otherwise, you will get a list.

           

          You can loop throught a list.

          • 2. Re: Quetion regarding checkboxes and inputting information
            BKBK Adobe Community Professional & MVP

            In the form:

             

            Use distinct names for the checkboxes, for example <cfinput type="checkbox" name="chkbx1">, <cfinput type="checkbox" name="chkbx2">, and so on.

             

             

            In the action page:

             

            As Dan has said, Coldfusion wont submit an unchecked checkbox.  So, do a cfparam for all the checkbox fields, for example

             

            <cfparam name="form.chkbx1" default="0">

            <cfparam name="form.chkbx2" default="0">

            and so on.

            • 3. Re: Quetion regarding checkboxes and inputting information
              Smdsports.com Level 1

              Thanks for the help.  The checkboxes on the form are dynamic

               

              <td>
                          <cfif #doh.recordcount# is 0>        
                          <input name="belt" type=checkbox value="#belt.belttotemrecord#">
                          <cfelse>
                          <font class="normal">X - #dateformat(doh.datereceived,'mm/dd/yy')#</font>
                          </cfif></td>

               

              The action page I need to loop through but not sure how to do it.  How do I pass through a variable to set a loop?  I think if I know how many boxes have been checked I can then set a loop variable, no?

               

              Sharon

              • 4. Re: Quetion regarding checkboxes and inputting information
                BKBK Adobe Community Professional & MVP

                On the form page, give each checkbox a unique name:

                 

                <cfset index = 1>

                <cfif doh.recordcount is 0>
                          
                            <input name="belt#index#" type="checkbox" value="#belt.belttotemrecord#">
                            <cfelse>
                            <font class="normal">X - #dateformat(doh.datereceived,'mm/dd/yy')#</font>
                </cfif></td>

                <cfset index = index + 1>

                 

                 

                 

                On the action page, count the number of checkboxes. Remember Coldfusion doesn't submit unchecked checkboxes.
                <cfset numberOfCheckboxes = 0>
                <cfloop collection="#form#" item="field">
                    <cfif left(field,4) is 'belt'>
                        <cfset numberOfCheckboxes = numberOfCheckboxes + 1>
                    </cfif>
                </cfloop>

                • 5. Re: Quetion regarding checkboxes and inputting information
                  Howannoying

                  This is beautiful...last stupid question....what an i inputting into the db now.  The name is now dynamic...its changing...so  I can no longer simply use

                  #form.belt# in my query...

                  • 6. Re: Quetion regarding checkboxes and inputting information
                    BKBK Adobe Community Professional & MVP

                    You could do it within the same loop, like this

                     

                    <cfset numberOfCheckboxes = 0>
                    <cfloop collection="#form#" item="field">
                        <cfif left(field,4) is 'belt'>
                            <cfset numberOfCheckboxes = numberOfCheckboxes + 1>

                            <cfset nameOfField = field>

                             <!--- output test --->

                            <cfoutput>#nameOfField#</cfoutput><br>
                        </cfif>
                    </cfloop>

                    • 7. Re: Quetion regarding checkboxes and inputting information
                      Dan Bracuk Level 5

                      BKBK is showing you the hard way.  If you want to use that method, at the end of the day, you use array notation or another string function to get the value of the form field.

                       

                      <cfloop collection="form" item="field">

                      <cfif left(field, 4) is "belt">

                      YourValue will either be form[field] or right(field, len(field) - 4) and what you do with that value is up to you.

                       

                      If you go back to your original idea of giving all the checkboxes the same name with different values, your code becomes simpler.  First, you don't need any cfparam tags.  You just do this.

                       

                      <cfif StructKeyExists(form, "belt">

                      <cfloop list = "#form.belt#" index = "field">

                       

                      Your value is then field and what you do with it is up to you.

                      1 person found this helpful
                      • 8. Re: Quetion regarding checkboxes and inputting information
                        Smdsports.com Level 1

                        That completley fixed it and its working beautifully..I cannot thank you enough and to all that offered assistance!!!!