-2

I have some information from my db that is being loaded onto my page, but I would like to be able to use an if statement regarding the value of this, and then assign a javascript value to be a boolean value. How can I go about this? (at the moment I am just printing the value)

<?php 
    if ($_SESSION['loggedin'] = true){
    print_r($_SESSION['userlevel']); // THIS IS WHAT I WANT TO SPECIFY IN AN IF STATEMENT
    }
?>

<script>
var userloggedin = false;
</script>

What I would like to do in pseudocode:

<script>
var userloggedin = false;

function somefunction(){
    if (userloggedin == true){
      //Do stuff...//
     }
}
</script>

Sorry for my lack of knowledge on the subject, I'm only beginning to learn backend web development.

fchuygy
  • 1
  • 1

5 Answers5

1

Have you tried searching the forum for any previous posts with regards to parsing PHP variables to javascript?

With a simple search I found a feed relating to parsing PHP variables to JavaScript here:
Get variable from PHP to JavaScript

Anyway, from my understanding of your problem does this serve as a suitable answer?

<?php 
    if ($_SESSION['loggedin'] == true){
        $userLevel = $_SESSION['userlevel'];
        $userLoggedIn = true;
    }
    else {
        $userLevel = null;
        $userLoggedIn = false;
    }

?>

<script type="text/javascript">
    var userLoggedIn = "<?php Print($userLoggedIn); ?>";
    var userLevel = "<?php Print($userLevel); ?>";

    if (userLoggedIn == true) {
        if (userLevel == "levelOne") {
            //Your code here
        }    
        else if (userLevel == "levelTwo") {
            //Your code here
        }
        else {
            //Your code here
        }
    }
</script>
Community
  • 1
  • 1
JackWinch
  • 11
  • 3
0

You can set a javascript variable with a php value by:

<script>
    var jsVar='<?php echo $phpVar ?>';
</script>
Mohammad Mehdi Habibi
  • 1,601
  • 1
  • 14
  • 30
0

You can echo bits of Javascript code from PHP, like this:

<script>
    var userloggedin = <?php echo ($_SESSION['loggedin'] ? 'true' : 'false'); ?>;
</script>
Joost
  • 4,094
  • 3
  • 27
  • 58
0

First change your if statement to use == double equal signs if you want the expression to work as expected. Then you can simply echo a variable into javascript as in example below:

Simple solution would be similar to:

<?php 
    if ($_SESSION['loggedin'] == true){
        $logged_in = "true";
    } else {
        $logged_in = "false";
    }
?>

<script>
    var userloggedin = <?php echo $logged_in;?>;
</script>
Kypros
  • 2,997
  • 5
  • 21
  • 27
0

A simple solution would be just to echo variable value in script tag. But this is unsafe, because variable is global, meaning it can be modified in client-side console.

Better solution would be an ajax request and php script that returns some response, this way the variable is scoped to ajax response function, so it's safe from modifications. Here's an example:

...
echo json_encode($_SESSION['loggedin'] ? 'true' : 'false');
...

In javascript, you can use jquery .get() request:

$.get("userLoggedIn.php", function(data) {
    var response = JSON.parse(data);
    if(data === true) {
        // user is logged in
    }
    else {
        // not logged in
    }
});

If it's important that this functionality is handled before anything else, an synchronous ajax request:

$.ajax({
    url: "userLoggedIn.php",
    async: false, // this makes request synchronous 
    success: function (data) {
        var response = JSON.parse(data);
        if (data === true) {
            // user is logged in
        } else {
            // not logged in
        }
    });

Synchronous request means that client will wait for request to finish before doing anything else. It's not recommended to be used a lot, but sometimes it's the only way to go about.

Marko Gresak
  • 7,950
  • 5
  • 40
  • 46