PHP show/hide <div>

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
kenn213
Forum Newbie
Posts: 2
Joined: Sat Nov 06, 2010 6:08 pm

PHP show/hide <div>

Post by kenn213 »

Hey guys, had a quick PHP question. I've been using a small PHP script to authenticate logged in users which redirects them to another page if they're not logged in. I'd like to change it so that instead of redirecting them to a different page, it hides a <div> element on my page, and shows another which pops up on top. I have my <div>'s, so all I need is to edit the PHP code now. Here is the code I currently have:

PHP at the top
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main.html");
}
?>

HTML
<div class="hsidebar1">
<div class="loginpanel">
Log in | Sign Up
</div>
<div class="loggedinpanel">
Logged In
</div>

<!-- End hSidebar1-->
</div>

CSS
.loginpanel {
visibility:hidden;
}

.loggedinpanel {
}


Basically I'd like the PHP to switch the visible <div> with the hidden one. If anyone has any ideas how to do this please let me know. I know how to show and hide <div>'s with javascript, but I'm not very experienced with PHP.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP show/hide <div>

Post by JakeJ »

PHP is server side so it would require reloading the page in order to accomplish what you want to do. You're better off using javascript for that.

Only CSS or js can hide a div. PHP can only either output it or not depending on what the code does.

Code: Select all

If ($a = 1) {
   echo "<div>Some content</div>";
Else {
   echo "<div>Some other content</div>";
}
But there is no hide/show div in php.
kenn213
Forum Newbie
Posts: 2
Joined: Sat Nov 06, 2010 6:08 pm

Re: PHP show/hide <div>

Post by kenn213 »

I understand php is server side and I'm pretty sure what I'm trying to do is possible. When the page loads I basically want it to show one of two divs.

I was thinking I could do it with variables. If I could change the PHP code I have so that instead of redirecting someone, it set $divone and $divtwo to either hidden or visable, depending on whether or not my if statement passes, and then I could use some code like:

<div class="hsidebar1">
<div style="visibility: <?php echo($divone); ?>" class="loginpanel">
Log in | Sign Up
</div>
<div style="visibility: <?php echo($divtwo); ?>" class="loggedinpanel">
Logged In
</div>

I'm just not sure how to make the output of my original PHP to set a variable, or how exactly all the syntax works. Basically this is what I need the syntax for:

<?
session_start();
if(!session_is_registered(myusername)){
** SET MY TWO VARIABLES HERE**;
}
?>
Post Reply