5 Replies Latest reply on Jul 15, 2009 10:03 AM by CFMLGuru

    Checkboxes and CF

    emmim44 Level 1

      Hi all, I have  populated checkboxes as follow in a form:

      < input type='checkbox' name='Admin_1' value=''>

      < input type='checkbox' name='Admin_2' value=''>

      < input type='checkbox' name='Admin_3' value=''>....

       

      < input type='checkbox' name='View_1' value=''>

      < input type='checkbox' name='View_2' value=''>

      < input type='checkbox' name='View_3' value=''>....

       

      ...I need to know how to write a cfscript/javascript to check if checkboxes are checked...Then I need to get the first part of  "_"....then update my db tb field

      with that new value.

      First part of the "_" is a unique key in db..

       

      Example: 

      if one checks off admin_1 and admin_3, the string value will be 101 and vice versa... Any help appreciated....

        • 1. Re: Checkboxes and CF
          Dan Bracuk Level 5

          You are really doing things the hard way.  It would be much easier if you gave all the checkboxes in each group the same name and different values.

           

          I don't quite understand how you get 101 from admin_1 and admin_3.

          • 2. Re: Checkboxes and CF
            emmim44 Level 1

            Assume each set has same name, then how would you do that?....

            and example: 101 means first checkbox and third box are checked not the 2nd...1 means if it is check 0 not check....

            • 3. Re: Checkboxes and CF
              CFMLGuru Level 1

              Emmim,

               

              Dan was correct in saying that you are doing it the hard way.  Checkboxes mean that there is a natural grouping.  If you want bit values represented whether 1 or 0, I would suggest putting this together on the ColdFusion side.  I would do this by looping through the code like this on the front end:

               

              <cfloop query="recordset">

                   <input type='checkbox' name='Admin' value=''#recordset.field#">

              </cfloop>

               

              This would create:

                   <input type='checkbox' name='Admin' value="1">

                   <input type='checkbox' name='Admin' value="2">

                   <input type='checkbox' name='Admin' value="3">

                   <input type='checkbox' name='Admin' value="4">

               

              on the action page do the following:

               

              <cfset Variables.myNewList = "_">

              <cfif isDefined("form.Admin")>

              <cfloop query="recordset">

                   <cfif Listfind(form.admin,recordset.fieldname)>

                         <cfset Variables.myNewList = Variables.myNewList & "1">

                   <cfelse>

                        <cfset Variables.myNewList = Variables.myNewList & "0">

                   </cfif>

              </cfloop>

              <cfelse>

                   <cfset Variables.myNewList = RepeatString(0,recordset.recordCount)>

              </cfif>

               

              I prepend the list with an "_" so that it will not convert the string to a number and thus remove any leading 0's.

               

              This will achieve what you are looking for.  However unless there is a VERY specific need to have the characters put in a certain position, I would strongly advise a modified approach.

               

              Sincerely,

               

              Braden

              • 4. Re: Checkboxes and CF
                emmim44 Level 1

                How would I know?

                 

                Lets say, admin checkbox 1 and 3 are checked...for checked boxes the value will be 1 and not checked 0... Then I need to populate the values of each set as lets say str= 101 then I will update the db field with this new value...

                • 5. Re: Checkboxes and CF
                  CFMLGuru Level 1

                  Copy and paste the code that I sent you into your pages.  It will work.