0

I have a PHP Session variable which can be 0 or 1 and this JS function:

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
        e.style.display = 'none';
    else
       e.style.display = 'block';
}

I would like this function to be run AUTOMATICALLY as soon as the page is opened only if the PHP Session Variable is = 0.

How and where can I create my If statement?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • The only way JS can check a PHP session variable in the manner you describe is to run an AJAX request. But this can easily be defeated (if the user disables javascript, for instance). Can you add a PHP snippet at the top of your page to check your session var instead? – WillardSolutions Feb 23 '16 at 23:47
  • Sorry I'm knew to this. What's a PHP snippet? –  Feb 23 '16 at 23:48
  • I'm not fussed by javascript disabling. I'm just looking for a simple code (even in Ajax) to do this –  Feb 23 '16 at 23:49
  • PHP is server-side, JavaScript is client-side. You can’t interact with the two. PHP has finished rendering the page by the time JavaScript gets involved. – Martin Bean Feb 24 '16 at 00:41

4 Answers4

1

One of the simpliest is just handle it like any normal code block. Add your condition in the if statement.

Basic idea:

<!-- head -->

<?php if($_SESSION['whatever'] === 0): ?>
<script type="text/javascript">

function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

toggle_visibility('body');

</script>
<?php endif; ?>

<!-- your HTML content, etc. -->

<!-- footer, etc. -->

If $_SESSION['whatever'] is not 0, after all is loaded, then you'll not see this JS code block in the page because remember PHP runs first.

Note: Don't forget to start the session.

Additional Note: Of course, another way would be to create an XMLHttpRequest. You'd need another PHP script that responds to this request. Just respond with a json_encoded response. This answer (vanilla) should give a better illustration.

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • @Snowmate just make sure it corresponds to the if block match `): ?>` (just make this is a colon not a semi colon) then content `` or the simplest flavor `` content `` i just like the former – Kevin Feb 24 '16 at 00:17
0

You could let PHP generate the JS code, you'd then get something like this

<script>
  <?php
    session_start();
    if($_SESSION["name"]=="value"){
      echo"document.getElementById('id').style.display='none';";
    }
  ?>
</script>
CerebralFart
  • 3,336
  • 5
  • 26
  • 29
  • 2
    Why "session_start()"? Do you mean this should be at the top of my page? Or simply where all my scripts go? –  Feb 23 '16 at 23:53
  • It should go where all your other `script`-tags are, hopefully inside the `head`. The `session_start` is there to start a session, if you have already started a session, you could remove this line. – CerebralFart Feb 23 '16 at 23:56
  • @ghost ok now no errors, but toggle_visibility won't run.. :( –  Feb 24 '16 at 00:23
0

You could use the following technique :

<?php
    if($_SESSION['shouldbezero'] === 0) {
        echo '<script src="shouldbezero.js"></script>';
    }
?>

The echo statement would run only if the value of $_SESSION['shouldbezero'] is equal to 0, and thus the <script> tag will only be added to your HTML code in that specific situation.

If you use this technique, you should also create a file named shouldbezero.js, which should contain all the JavaScript code you only want to run if the value of $_SESSION['shouldbezero'] is equal to 0.


Note :

If you only want to load CSS based on a certain value in your PHP code, could do the same thing with that CSS code.

For example, you could add echo <link rel="stylesheet" type="text/css" href="shouldbezero.css">'; right beneath the echo '<script src="shouldbezero.js"></script>';, as demonstrated in the code example below :

<?php
    if($_SESSION['shouldbezero'] === 0) {
        echo '<script src="shouldbezero.js"></script>';
        echo '<link rel="stylesheet" type="text/css" href="shouldbezero.css">';
    }
?>

If you do this, you should also create a file named shouldbezero.css, which should contain all the CSS code you only want to process if the value of $_SESSION['shouldbezero'] is equal to 0.

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • Hi @John thanks for the answer. I've done the separate JS file and call it from the html page, but still it won't run! I've added: toggle_visibility('popup-box1'); At the bottom of the JS code but it won't work. I am sure the Session variable is = 0, but the JS code won't run –  Feb 24 '16 at 11:39
  • @Snowmate : Can you put a demo online of what you're trying? Then I could try debugging it. – John Slegers Feb 24 '16 at 12:26
0
<?php
 $execJs = "false";
 session_start();
 if (isset($_SESSION['variable']) && $_SESSION['variable'] === 0) {
  $execJs = "true";
 }
?>
<script>
var id = 'whatever';
var execJs = "<?php echo $execJs ?>";

if (execJs === 'true') {
  toggle_visibility(id);
}

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
        e.style.display = 'none';
    else
       e.style.display = 'block';
}
</script>
Nick
  • 156
  • 1
  • 11