Really easy help (Cookies)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fizziwig
Forum Newbie
Posts: 2
Joined: Wed May 06, 2009 3:01 pm

Really easy help (Cookies)

Post by Fizziwig »

Okay, I have a page where sections are shown and hidden when a link is clicked.

What I want is to be able to save which sections visitors have open, and which they have closed, and I decided cookies was the best bet.

Here is the code, it is mainly Javascript / HTML currently, with PHP at the top.

Any help would be greatly appreciated.

Code: Select all

<?php
$expire=time()+60*60*24*30;
setcookie("para1", "$1", $expire);
?>
 
<html><head><title>Show / hide testing</title></head>
<body><script type="text/javascript">
 
function displayPara(fieldID, displayBol) {
 
  var paraObj = document.getElementById(fieldID);
  var showObj = document.getElementById(fieldID+'_show');
  var hideObj = document.getElementById(fieldID+'_hide');
 
  paraObj.style.display = (displayBol)? '' : 'none' ;
  showObj.style.display = (!displayBol)? '' : 'none' ;
  hideObj.style.display = (displayBol)? '' : 'none' ;
 
}
 
</script>
 
<a href="#" onclick="displayPara('para1', false);" id="para1_hide" style="display:none;">Hide</a>
<a href="#" onclick="displayPara('para1', true);" id="para1_show" style="">View</a>
<div id="para1" style="display:none;">1st paragraph of text</div>
 
<a href="#" onclick="displayPara('para2', false);" id="para2_hide" style="display:none;">Hide</a>
<a href="#" onclick="displayPara('para2', true);" id="para2_show" style="">View</a>
<div id="para2" style="display:none;">2nd paragraph of text</div>
 
<a href="#" onclick="displayPara('para3', false);" id="para3_hide" style="display:none;">Hide</a>
<a href="#" onclick="displayPara('para3', true);" id="para3_show" style="">View</a>
<div id="para3" style="display:none;">3rd paragraph of text</div>
</body></html>
 
 
Last edited by Benjamin on Wed May 06, 2009 5:37 pm, edited 1 time in total.
Reason: Changed code type from text to php.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Re: Really easy help (Cookies)

Post by staar2 »

Why not make your life easier use Jquery for ajax and support the serverside with sessions and php ?
Fizziwig
Forum Newbie
Posts: 2
Joined: Wed May 06, 2009 3:01 pm

Re: Really easy help (Cookies)

Post by Fizziwig »

If you can explain how to do that, sure.

I was basing it on languages that I knew, which doesn't include PHP.
Post Reply