简体   繁体   中英

How to Pass PHP Session Variable to external JS file in Symfony

Currently I am working in One PHP Symfony Project Regarding the Performance Evaluation of an Employee. So I need to know how we can get PHP session variable of an employee (Employee Role) Please see the php file ( $role = $_SESSION['empRole']; ) to an external js file.(js file also attached).I am using PHP Symfony framework.

Actually I need to check who is logged in to the website.if it is admin/core members / Hr. I need to apply validation to some fileds only when the Coremembers login the website.

Here is the code section that I need to pass $role to External JS file(in pastebin)

    public function executeIndex(sfWebRequest $request) {
        if (isset($_SESSION['empId'])) {
            $role = $_SESSION['empRole'];
            if ($role == "admin") {
                $empId = $this->getUser()->getAttribute('empId');
                $team_members = GroupwareEmployeesPeer::getAllSubordinatesId($empId`enter code here`);
                $nc = new Criteria();
                $nc->add(PeD`enter code here`ates`enter code here`Peer::NAME, 'Dashboard');
                $resultset = PeDatesPeer::doSelect($nc);

So Can you guys give me a solution regarding this, since I am stuck with this for a long time.

Please see thepastebin code for php and external js file.

https://pastebin.com/kbkLjSFa - PHP File.

https://pastebin.com/M60Rg64U - JS file

Yes you can get the php variable into the js file .

Method I:

Rename your.js file to.js.php and pass the php variable as a query string to this file where you link this file in your page.

So, if you have some test.js file, rename it to test.js.php and use it in your page like this

<script src="test.js.php?session_var=<?= $your_var_here;?>&any_var=<?= $any_php_var;?>"></script>

And in your js file, retrieve the values from query string parameters So inside test.js.php, you can simply

var some_var = "<?= $_GET['session_var']";
var any_var = "<?= $_GET['any_var'];?>";
//rest of your logic goes here

Method II:

Use an AJAX request.

var some_var, another_var; 
$.ajax({
  url : '/url-which-returns-session-vars', //return the session data in json from this url
  asnyc : false,
  dataType : 'json', 
  success : function(res){
     some_var = res.some_var;
     another_var = res.another_var;
  }
});
//rest of the logic goes here.

Both of these methods work. But I prefer the first method, since I don't have to write extra code.

Edit: Suppose the response of the ajax call looks like this

{
   some_var : 'value here',
   another_var : 'another value here'
}

So res in the success function argument contains this response and since the response is a JSON you can access these values using the . notation . Thus,

some_var = res.some_var;
another_var = res.another_var;

and since these variables are global variables, you can use them anywhere in your code in this file.

 ?> <script> var php_variable = '<?php $variable ?>'; </script> <?php

Global variable should be declared before external js file include . in js file access it using this variable name php_variable

After the above code. jquery external file should be included here

<script src="external.js" ></script>

external.js

consol.log(php_variable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM