Skip to main content
July 10, 2007
Answered

Help with simple checkbox form

  • July 10, 2007
  • 3 replies
  • 608 views
Hi all, I'm trying to create a little checkbox form that when clicked will save the "check" (boolean?) to a variable so when the page reloads it remains checked (and if then unchecked remains unchecked).

I'm totally new to Coldfusion so far have only come up the variable declaration?

<cfparameter name="IsBoxChecked" default = "no">

I know the html for checkboxes looks like whats below, but how can I add the CF tags to make it do what I want?

<input type="checkbox" name="IsBoxChecked" value="1"> MyValue
This topic has been closed for replies.
Correct answer efecto747
Hi, maybe you could save the checkbox selections in a structure and store the structure as a session variable so it remains visible for the duration of the user's browser session.

To use session variables you must turn on session management in application.cfm (or cfc). Have a look at the code snippets below for examples on how you would do this:

3 replies

Participating Frequently
July 11, 2007
Use the preservedata attribute of the cfform tag.

Try the below:

<cfoutput>
Was the checkbox checked on the last refresh #IsDefined("form.refreshCheck")#<br>
</cfoutput>
<cfform method="post" preservedata="yes">
Stay checked on Refresh
<cfinput type="checkbox" name="refreshCheck">
<cfinput type="submit" name="refresh" value="refresh">
</cfform>
Inspiring
July 11, 2007
The cookie suggestion was based on what appears to be a wrong interpretation of the original question. I thought you meant that if the user came back to the page 3 days later, and had turned his computer off in between, you wanted your program to remember whether or not he had checked the box.

Should you ever want to do that, cfcookie is the way to go.
Inspiring
July 10, 2007
Use cfcookie to keep track of the last selection.

Read the cookie each time you load the page. Then do something like this.

<input type="checkbox"
<cfif WasCheckedLastTime checked="checked"
name="IsBoxChecked" value="1"> MyValue
July 11, 2007
I don't think cfcookie is really what I'm looking for. I want it to save to a variable because I will use that variable in other statements in the same template. For example, if they check the box, I want to be able to add an additional column to a sql query. Also, I want them to be able to change many of them change the output of the query(ies).

Thanks for your help
efecto747Correct answer
Participating Frequently
July 11, 2007
Hi, maybe you could save the checkbox selections in a structure and store the structure as a session variable so it remains visible for the duration of the user's browser session.

To use session variables you must turn on session management in application.cfm (or cfc). Have a look at the code snippets below for examples on how you would do this: