Issue Displaying Div (javascript and php)

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
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Issue Displaying Div (javascript and php)

Post by HiddenS3crets »

here's the basic structure in sudo code

Code: Select all

<script type='text/javascript'>
if (box is hidden)
  show box
  update cookie to say box is now visible
else
  hide box
  update cookie to say box is now hidden
</script>

<body>
<?php
if(cookie for box)
  display box
?>
</body>

the cookie is a boolean value. the problem is that when i click a link to show the box, the box does not display because even though i update the cookie to say 'show the box', the php code below has already executed and read to hide the box (if i refresh the page the box will show because the php code reads the updated cookie value).

I understand why it's happening, but can't figure out a solution to combat this. any ideas?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Issue Displaying Div (javascript and php)

Post by markusn00b »

Use javascript to display the div.

Code: Select all

 
document.getElementById('theDiv').style.display = 'block';
 
Mark.

P.S. Pseudo code* ;)
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Re: Issue Displaying Div (javascript and php)

Post by HiddenS3crets »

fixed it :D just needed to add a couple extra lines of code

Code: Select all

$(document).ready(function()
{
  // start additional lines
  if(getCookie('toggle_box') == 0)
  {
    $('#toggle_box').css('display', 'none');
  }
  else
  {
    $('#toggle_box').css('display', 'inherit');  
  }
  // end additional lines
  
  $('#toggle_button').click(function()
  {
    // if visisble, hide it
    if($('#toggle_box').is(':visible'))
    {
      document.cookie = "toggle_box = 0";
      
      $('#toggle_box').slideUp('slow');
      $('#toggle_button').text('SHOW');
    }
    // show it
    else
    {
      document.cookie = "toggle_box = 1";
      
      $('#toggle_box').slideDown('slow');
      $('#toggle_button').text('HIDE');
    }
  });
});
Post Reply