user5738346
user5738346

Reputation:

How to run if statement from PHP to run JS function

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?

Upvotes: 0

Views: 1238

Answers (4)

John Slegers
John Slegers

Reputation: 47081

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.

Upvotes: 0

Nick
Nick

Reputation: 156

<?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>

Upvotes: 0

Kevin
Kevin

Reputation: 41885

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.

Upvotes: 1

CerebralFart
CerebralFart

Reputation: 3490

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>

Upvotes: 0

Related Questions