2

EDIT: thanks a lot for all your comments/replies. Even if i remove the JS, and use the PHP if/else statements only, i still get the same error.

i keep on running into the same error:

The 2 divs below are visible whereas only one of them should be shown at a time (whether or not the user is logged in).

For simplicity purposes, i removed the html inside the 2 divs.

<div id="header_wrapper">

<div id="loginbutton"></div>

<div id="menu">
<ul>
<li></li>
<li></li>
<li></li>    
</ul>
</div>

<?php 
if (!isset($_SESSION['id']) || empty($_SESSION['id'])){  ?>

<script>
document.getElementById("menu").style.display = "none";
document.getElementById("loginbutton").style.display = "show";
</script>
<?php }
else {  ?>
<script>
document.getElementById("loginbutton").style.display = "none";
document.getElementById("menu").style.display = "show";
</script>

<?php 
}
?>
</div>

The $_SESSION['id'] is not empty. Indeed if i echo $_SESSION['id'] on the page, i get the correct session #.

Am I missing something?

7
  • Don't use else, use if (isset($_SESSION['id'])) and btw, it should be ...dislpay = "visible"; Commented Jun 27, 2013 at 5:40
  • I'm not sure if this will matter but try changing <script> to <script type="text/javascript"> Commented Jun 27, 2013 at 5:41
  • thanks for your help, it's still buggy, going to check the js console Commented Jun 27, 2013 at 5:59
  • Once you are logged in, there's no way to log out? Commented Jun 27, 2013 at 6:09
  • Change <? to <?php what happens? Commented Jun 27, 2013 at 6:16

4 Answers 4

3

If you use session like following exaple you can hide the div

<?php if(isset($_SESSION['id'])) { ?><div>div content</div><?php }?>
Sign up to request clarification or add additional context in comments.

Comments

2

What about this if you put your html inside php if blocks , or it is necessary to use javascript you can do it also by using only php then why adding JS

<div id="header_wrapper">
<?php 
if (!isset($_SESSION['id']) || empty($_SESSION['id'])){  ?>
<div id="loginbutton"></div>
<?php }
else {  ?>
<div id="menu">
<ul>
<li></li>
<li></li>
<li></li>    
</ul>
</div>
<?php 
}
?>
</div>

4 Comments

PHP is what I tried at first, but no luck, it still shows the 2 divs at the same time.
How in if/else only one condition will run how both conditions run at same time think man ?
Thanks a lot for your input, i don't understand either ! i ve never had such a problem before !
This is the correct answer, but you have to use another iftest. When the user logs in, set $_SESSION['LoggedIn'] = true. Then you can check if(isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn']) { around the <div>
1

Change

.style.display = "show";

TO

.style.display = "block"; 

OR

.style.display = ""; 

AND change

<script>

TO

<script type="text/javascript">

There is no valid value called show in this context. If you have Firebug (or similar) check in the console for further js-errors. If you don't, download it right away :-)

4 Comments

<script> alone is valid under HTML5
@random - yes, but there's nothing indicating it's a html5-document.
thanks for your help, it's still buggy, going to check the js console
@CharlieHarris - you're welcome. What message/warning/errors do you get?
1

Edit:

<div id="loginbutton"></div>

<div id="menu">
<ul>
<li></li>
<li></li>
<li></li>    
</ul>
</div>

<?php 
if (isset($_SESSION['id']) && !empty($_SESSION['id'])){  ?>
<script>
document.getElementById("loginbutton").style.display = "none";
document.getElementById("menu").style.display = "show";
</script>
<?php }
else {  ?>

<script>
document.getElementById("menu").style.display = "none";
document.getElementById("loginbutton").style.display = "show";
</script>

<?php 
}
?>
</div>

this should work

4 Comments

can you tell me what exactly is happening? like when a user is logged in what is being shown?
EDIT: When the user is logged in, both the divs are visible. As soon as the user logs out, only #menu is visible whereas it should be #loginbutton
try making it 'and' instead of 'or' in the if statement
try my updated code and are u deleting the sessions after the user logs out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.