Solved

Using a Checkbox to clear cookies

Posted on 2013-06-26
12
285 Views
Last Modified: 2013-09-20
Hi,

I currently have a page that shows a 'hint' when the browser detects a presence of a coldfusion cookie. I have always wondered how those web pages work when there is a checkbox that states that if I check the box, the hint would not show anymore the next time the page loads. I am hoping to accomplish the same thing.

Currently, when a person signs up to login to the site, I create a cookie that never expires called 'newmember' and a hint would show up on the welcome page.

<cfif IsDefined('cookie.newmember')>
      
<div>This is a hint</div>

</cfif>

I was wondering how I could place a checkbox on the page to state that 'If you check this checkbox, this hint will not show again'. So, once the user checks the box, the cookie 'newmember' will be deleted....

Thanks very much for any insights.
0
Comment
Question by:CFbubu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39278598
If coldfusion works like php you would set the cookie or I like to use sessions instead. On the visitor's next visit to the site you could read the cookie or the session for this data checkpoint and have your code act on the data you set in the cookie or session.

Note: I am not a coldfusion persion. Mostly php and perl but the method should work the same.
0
 

Author Comment

by:CFbubu
ID: 39278698
Hmm...but I would like to have the 'hints' show always until the user clicks on a checkbox to indicate they would not like the hints to show anymore. Thanks though for your feedback.:)
0
 
LVL 7

Assisted Solution

by:Robert Saylor
Robert Saylor earned 250 total points
ID: 39278719
I think it would be a easy process to set a cookie with the checkbox. Look into ajax.

Here is some information on coldfusion cookies. The act just like php cookies just the syntax is different.

http://stackoverflow.com/questions/3265516/how-do-you-read-cookie-values-in-coldfusion-that-have-special-characters-in-the

Then when they return to the site read the cookie and use a "if" statement to display the hints or not.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39279067
0
 

Author Comment

by:CFbubu
ID: 39279508
I was wondering if how this could be done with ajax and coldfusion. That means when the user checks the checkbox, the form is submitted without a page refresh. The page that the data is submitted to than wipes the cookie.......

Is this possible?
0
 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39279672
Ajax is JavaScript related so the backend should not matter. As long as you can accept post and get data...
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 250 total points
ID: 39279747
You could remove the cookie, but it's simpler to toggle it on/off.

Like rsaylor said, you need ajax to do it without a page refresh.  Here's a rough example using a bind.  

Test.cfm
-----------
<cfparam name="cookie.showHint" default="true">

<cfdump var="#cookie#" label="Debug: Cookies on page load">
<cfform>
   Always show hints? <cfinput type="checkbox" name="alwaysShow" checked="#cookie.showHint#">
   Debug Ajax call (should be type=hidden)
   <!--- change cookie state when checkbox state changes --->
   <cfinput type="text" name="updateHint"
              bind="cfc:path.to.YourComponent.toggleHint({alwaysShow.checked@click})">
   <input type="submit">
</cfform>


YourComponent.cfc
-------------------------------
<cfcomponent>
    <cffunction name="toggleHint" access="remote" returntype="string">
           <cfargument name="show" type="string" default="true">
           <!--- toggle cookie value:  true / false --->
           <cfcookie name="showHint" value="#arguments.show#">
           <cfreturn arguments.show>
    </cffunction>
</cfcomponent>
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39279853
You're all trying too hard.  Changing the value of a cookie for a user or even expiring it is the same as setting it but with different values.  Javascript alone can do this without going back to the server at all.  http://www.w3schools.com/js/js_cookies.asp  sample code.
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

Open in new window

0
 

Author Comment

by:CFbubu
ID: 39279953
Thanks for all your comments and kind solutions :)

Hi _agx_!

Thanks again for you awesome suggestion. I am interested in your concept of turning the cookie on/off. I tried to use your rough solution, ...copied and pasted your sample code, but I keep encountering an error:

"CFC function not found in the bind expression cfc:toggleHint({alwaysShow.checked@click}) "

I did update the bind path as seen below as I created a toggleHint.cfc which housed the component code inside the same folder as the test.cfm page

<cfinput type="text" name="updateHint"
              bind="cfc:toggleHint({alwaysShow.checked@click})">

Also, as the page kept giving an error, I assume that this works without the requirement of the submit button...

I hope you can guide me where I went wrong trying your sample code example.

Thanks so much!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39279963
Looks like you're missing the component name.  The bind path must include the component and function name.  BUT @Dave's right.  There's really no reason to use CF for this.  I just didn't have time to dig up any javascript code.  Try calling his function from the bind.  Just use bind="javascript:..."  instead of bind="cfc:..".
0
 

Author Comment

by:CFbubu
ID: 39279975
Sorry.

I am alittle confused now....

I am not sure how to put it together to use as a working sample for learning. I do not see how Dave's example enables a user to click on a checkbox which will cause a hint to be displayed or not. Since this kind of control has to be up to the user and not when the cookie has been programmed to expire...
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39280023
You asked about the cookie, not about making something hidden.  But you can do that with javascript also.  Also, if you delete the cookie, you have no way of knowing that it was ever set and that something was clicked to hide the notice.  Here is a simple example.  You can put the checkbox inside the 'div' too so it disappears if you want.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
// hide div
document.getElementById('theHint').style.visibility = 'hidden';
document.getElementById('theHint').style.display = 'none';
}
// -->
</script>
</head>
<body>


<div id="theHint">
<input type="checkbox" onclick="setCookie('newmember','off',10000)" />If you check this checkbox, this hint will not show again.<br />
This is a hint</div>
</body>
</html>

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question