PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] How to Pass PHP Session Variable to external JS file in Symfony

 February 01, 2022     javascript, php, session, symfony     No comments   

Issue

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


Solution

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.



Answered By - Wdy Dev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing