10 Replies Latest reply on Apr 9, 2009 10:27 AM by john85

    Remembering checkbox

    thnguyen

      <a href="index.cfm?goto=A">A</a> |

      <a href="index.cfm?goto=B">B</a> |

      <a href="index.cfm?goto=C">C</a> |

      <a href="index.cfm?goto=D">D</a> |

      <a href="index.cfm?goto=E">E</a> |

       

      I have a top navigation that will display a certain letter.  There are checkbox under each letter.  I am using session variable to remember the checkbox.

       

       

       

      <input name="general_1" type="checkbox" value="1" />
      <input name="general_2" type="checkbox" value ="2" />
      <input name="general_3" type="checkbox" value="3" />
      
      

      What is the best way for session variable to remember if the checkbox is checked or not when user click on different letter and go to different view?

       

      Thanks

        • 1. Re: Remembering checkbox
          john85 Level 1

          Why not cookie?

           

          After a few minutes reading the sample below,security wise is bad. Itwould allow "user" to set/change other session variables which is bad.

           

          Do you want to set the session when the checkbox is clicked? If so, I think you need to detect the onclick event on the checkbox and use ajax to set the session key.

           

           

          <script language="javascript" type="text/javascript">
               function setSession(objCheck){
                    var xmlObject = null;

                    try {
                        xmlObject = new XMLHttpRequest();
                    } catch(e){
                         try {
                              xmlObject = ActiveXObject("Msxml12.XMLHTTP");
                         } catch (e2){
                              try {
                                   xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
                              } catch (e3) {xmlObject = null;}
                         }
                    }

                    if (xmlObject != null) {
                       xmlObjec.open("post", "somePage.cfm?name=" + objCheck.name + "&isChecked=" + objCheck.checked, true);
                       //and so forth
                       xmlObject.send(null);
                    }
               }
          </script>

          <input name="general_1" type="checkbox" value="1" onclick="setSession(this)" <cfif isDefined("SESSION.general_1") AND UCase(SESSION.general_1) EQ "ON">checked</cfif>/>

           

          somePage.cfm..

          <cfif isDefined("URL.name") AND isDefined("URL.isChecked")>

             <cfset temp = StructInsert(SESSION, URL.name, "OFF"/>

             <cfif UCase(URL.isChecked) EQ "TRUE">

                     <cfset temp = StructInsert(SESSION, URL.name, "ON"/>

             </cfif>

          </cfif>

          • 2. Re: Remembering checkbox
            thnguyen Level 1

            I am not good with ajax, unfortunately.  Basicially, the checkbox is used to filter the results from database.  So I want to use session var to remember which checkbox is checked.  If user wants to view 'A' with checkbox 1 checked, then A will be filtered 1 instead of displaying everything related to 'A'.  And every letter view with be filtered 1 until user uncheck that checkbox then will displaying everything without the filter.  The code below is basically what i want to do in 1 cfm page.  A session var or something that would remember the checkbox checked and the variable to a different letter and filter in the database.

             

            <cfparam name="filter" default="" />
            
            <cfif isdefined("form.filter_1")>
                 <cfset filter = filter &'1,' />
            </cfif>
            <cfif isdefined("form.filter_2")>
                 <cfset filter = filter &'2,' />
            </cfif>
            <cfif isdefined("form.filter_3")>
                 <cfset filter = filter &'3,' />
            </cfif>
            
            
            
            <a href="index.cfm?goto=A">A</a> | <a href="index.cfm?goto=B">B</a> | <a href="index.cfm?goto=C">C</a>
            
            
            
            
            <form name="form1" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="post">
                 <input name="filter_1" type="checkbox" value="1" onClick="form1.submit();" <cfif isdefined(filter_1) OR listfind(filter, 1) EQ 1>checked</cfif> />
                 <input name="filter_2" type="checkbox" value="2" onClick="form1.submit();" <cfif isdefined(filter_2) OR listfind(filter, 2) EQ 2>checked</cfif> />
                 <input name="filter_3" type="checkbox" value="3" onClick="form1.submit();" <cfif isdefined(filter_3) OR listfined(filter,3) EQ 3>checked</cfif> />
            </form
            
            <cfquery name="get_data" datasource="ds">
                 SELECT * FROM table
                 WHERE rc IN (#filter#)
            </cfquery>
            
            <cfoutput query="get_data">
                 #data#
            </cfoutput>
            
            • 3. Re: Remembering checkbox
              john85 Level 1
              <cfparam name="filter" default="" />

              <cfif isdefined("form.filter_1")>
                   <cfset filter = ListAppend(filter, 1, ",")/>
                   <cfset SESSION.fitler_1 = "ON"/>
              <cfelse>
                   <cfset SESSION.fitler_1 = ""/>
              </cfif>

              <cfif isdefined("form.filter_2")>

                   <cfset filter = ListAppend(filter, 2, ",")/>
                   <cfset SESSION.fitler_2 = "ON"/>
              <cfelse>
                   <cfset SESSION.fitler_2 = ""/>
              </cfif>
              <cfif isdefined("form.filter_3")>    

                   <cfset filter = ListAppend(filter, 3, ",")/>
                   <cfset SESSION.fitler_3 = "ON"/>
              <cfelse>
                   <cfset SESSION.fitler_3 = ""/>
              </cfif>


              <a href="index.cfm?goto=A">A</a> | <a href="index.cfm?goto=B">B</a> | <a href="index.cfm?goto=C">C</a>




              <form name="form1" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="post">
                   <input name="filter_1" type="checkbox" value="1" onClick="form1.submit();" <cfif (isdefined(SESSION.filter_1) AND UCase(SESSION.filter_1) EQ "ON") OR listfind(filter, 1) GT 0>checked</cfif> />
                   <input name="filter_2" type="checkbox" value="2" onClick="form1.submit();" <cfif (isdefined(SESSION.filter_2)
              AND UCase(SESSION.filter_2) EQ "ON") OR listfind(filter, 2) GT 0>checked</cfif> />
                   <input name="filter_3" type="checkbox" value="3" onClick="form1.submit();" <cfif (isdefined(SESSION.filter_3)
              AND UCase(SESSION.filter_3) EQ "ON") OR listfind(filter, 3) GT 0>checked</cfif> />
              </form

              <cfquery name="get_data" datasource="ds">
                   SELECT * FROM table
                   WHERE rc IN (#filter#)
              </cfquery>

              <cfoutput query="get_data">
                   #data#
              </cfoutput>
              • 4. Re: Remembering checkbox
                john85 Level 1

                That won't work (only allow one filter). Maybe instead of submitting the form directly onclick, user has to click a button after they check all the checkbox that they want.

                 

                I am other user would have better ideas, but replace the old form with this one should work.

                <form name="form1" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="post">
                     <input name="filter_1" type="checkbox" value="1" <cfif (isdefined(SESSION.filter_1) AND UCase(SESSION.filter_1) EQ "ON") OR listfind(filter, 1) GT 0>checked</cfif> />
                     <input name="filter_2" type="checkbox" value="2" <cfif (isdefined(SESSION.filter_2)
                AND UCase(SESSION.filter_2) EQ "ON") OR listfind(filter, 2) GT 0>checked</cfif> />
                     <input name="filter_3" type="checkbox" value="3" <cfif (isdefined(SESSION.filter_3)
                AND UCase(SESSION.filter_3) EQ "ON") OR listfind(filter, 3) GT 0>checked</cfif> />
                     <input type="submit" value="Filter"/>
                </form
                • 5. Re: Remembering checkbox
                  thnguyen Level 1

                  thanks...it would be nice to use onclick instead of click button.  That's the only idea that i have and somehow remember the checkbox and its value to filter out the database.  Let's me try and test to see if anything else or any other way to solve this problem...

                  • 6. Re: Remembering checkbox
                    john85 Level 1

                    Actually, onClick should work! My bad.

                    • 7. Re: Remembering checkbox
                      thnguyen Level 1

                      thanks again...it still not carrying and remember checkbox checked or not...I have to find another way, I guess...It shouldn't be that hard but my brain is dead on this problem.

                      • 8. Re: Remembering checkbox
                        john85 Level 1

                        Maybe I am thinking differently, but I tested the code and it has no problem remembering which checkbox is checked and which is not.

                         

                        edited: It won't retain the data when the page is load/reload (but works via submitted).

                         

                        This should work.

                         

                         

                        <cfif isDefined("FORM.filter_1")>

                             <cfset filter = ListAppend(filter, 1, ",")/>

                             <cfset SESSION.filter_1 = "ON"/>

                        <cfelseif isDefined("FORM.formSubmitted")>

                             <cfset SESSION.filter_1 = ""/>

                        </cfif>

                         

                        ....

                         

                        <form name="form1" ....>

                             <input name="formSubmitted" type="hidden" value="1"/>

                             <input name="filter_1" .../>

                             ...

                        </form>

                        1 person found this helpful
                        • 9. Re: Remembering checkbox
                          thnguyen Level 1

                          thanks for your help.  I eventually with a suggested from someone using a form with javascript to pass variables over and to remember checkbox checked. See the code below.  it works for what I need.  Thanks again for your help and suggestions.  I appreciate it!!

                           

                          <cfparam name="filter" default="" />
                          <cfparam name="getLetter" default ="" />
                          <script>
                          function sub(letter)
                               { document.getElementById("getLetter").value= letter;
                               document.name.submit();
                               }
                          </script>
                          <form name="name" id="name" action="#cgi.script_name#" method="post">
                               <a href="javascript:sub('A');">A</a> | <a href="javascript:sub('B');">B</a>
                               <input type="checkbox" name="general_1" value="1" <cfif isdefined("form.general_1") OR listfind(filter, 1) EQ 1>checked</cfif> onClick="name.submit();" />
                               <input type="checkbox" name="general_2" value="2" <cfif isdefined("form.general_2") OR listfind(filter, 2) EQ 2>checked</cfif> onClick="name.submit();" />
                               <input type="hidden" id="getLetter" name="getLetter" value="getLetter" />
                          </form
                          
                          • 10. Re: Remembering checkbox
                            john85 Level 1

                            Glad that you find a solution, I didn't know that you want to be able to click on the <a href="... link