CFbubu
asked on
Using a Checkbox to clear cookies
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.newmembe r')>
<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.
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.newmembe
<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.
ASKER
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.:)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
JavaScript Cookies
http://www.w3schools.com/js/js_cookies.asp
http://www.w3schools.com/js/js_cookies.asp
ASKER
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?
Is this possible?
Ajax is JavaScript related so the backend should not matter. As long as you can accept post and get data...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
ASKER
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@c lick}) "
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({alwa ysShow.che cked@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!
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
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({alwa
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!
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:..".
ASKER
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...
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...
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>
Note: I am not a coldfusion persion. Mostly php and perl but the method should work the same.