Skip to main content
Known Participant
April 7, 2009
Question

Remembering checkbox

  • April 7, 2009
  • 2 replies
  • 2398 views

<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

This topic has been closed for replies.

2 replies

Known Participant
April 8, 2009

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.

Inspiring
April 8, 2009

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>

Known Participant
April 9, 2009

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

Inspiring
April 7, 2009

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>

Known Participant
April 7, 2009

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>

Inspiring
April 8, 2009
<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>