1

How to store PHP session variable in JQuery variable.

I have one php file where i am using session variable as

$local_session = $_SESSION['sessionusername'];

and that PHP falls also using one .js file where i want to store this $local_session which is PHP variable to JQuery variable

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
Partap
  • 181
  • 1
  • 9
  • And your JS file is included on the top or bottom of the page ? – Abhik Chakraborty Jan 20 '14 at 11:11
  • Please consider looking at [How to put php inside javascript?](http://stackoverflow.com/questions/3345457/how-to-put-php-inside-javascript) or [Include PHP inside javascript (.js) file](http://stackoverflow.com/questions/3241422/include-php-inside-javascript-js-file). – phemios Jan 20 '14 at 11:11
  • jQuery is NOT A LANGUAGE! jQuery is a framework written in javascript! – Axel Amthor Jan 20 '14 at 11:12
  • @ Axel Amthor, we know this thing that Jqeury is not a language.. It is a framework – Partap Jan 20 '14 at 11:21

6 Answers6

1

It is as

session_start();
ob_start();
if(!$_SESSION['sessionusername'])
{
    header("location:index.php");
}
else
{
    include ('connection2.php');
    include('PHP/combo_val.php');
    $local_session = $_SESSION['sessionusername'];
 }

and after this my HTML code starts

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
Partap
  • 181
  • 1
  • 9
0

You might do something like this in your java script:

var sessionVar = '<?php echo $local_session; ?>';

and then use this global JS variable wherever in your page.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • This can break very easily depending on the value of the variable. Correct is `sessionVar = = json_encode(...) ?>;` -- note no explicit quotes around the value. – Jon Jan 20 '14 at 11:17
  • @ Axel Amthor I have done the same thing in JQuery Code as var lgn_usr = ; alert(lgn_usr); But in alert it displays as ; – Partap Jan 20 '14 at 11:18
  • I know this was 2014, although PHP echo short code: = Is no longer valid, I'm pretty sure it's getting removed in PHP 8, always use: – AdheneManx Jun 23 '20 at 03:46
0

You can write some php-script which return session data, for example

JS:

//my.js
$.getJSON( "myssesiondata.php", function( data ) 
{
     console.log(data);
}

PHP:

<?php
//mysession.php
return json_encode($_SESSION);
?>
0

@ Axel Amthor

My JQuery Code is as

 var lgn_usr  = '<?php  echo $_SESSION["sessionusername"] ?>';
 alert(lgn_usr);

and it display

   <?php  echo $_SESSION["sessionusername"] ?> 

as message

Partap
  • 181
  • 1
  • 9
  • That's not jQuery but pure Java Script. Your file is not interpreted as PHP by the server. Is that a `"...js"` file? Then try to rename it to `....php`, it should solve the problem (almost). – Axel Amthor Jan 20 '14 at 11:35
  • I assume that this `var lgn_usr = '';` is written somewhere in an external js file included in to the php file. This will not be seen by the PHP interpeter and thus this expression gets not interpeted and will be alerted out as it is by your Java Script. – Axel Amthor Jan 20 '14 at 11:44
  • Okay, now i got it.. But the point is that I have to use it in external .js file and I have to pass this usersession with other info to other PHP file for running DB Queries – Partap Jan 20 '14 at 11:47
  • The "other PHP file" will always have the entire Session, there' no need to pass things via Java Script as long as you don't want to change session values by Java Script. You need to have the file interpreted as PHP, so rename it to `....php` and include it with this ending. Or, better, go with some kind of AJAX solution handling the entire stuff server side, there are some answers already suggesting this below. – Axel Amthor Jan 20 '14 at 11:55
  • @ Axel Amthor. Yes, You are right.. I have already posted my post regarding this PHP sessions and JQuery... Now, I am going to follow the same method that all php file contains the same session value :P – Partap Jan 20 '14 at 11:58
0

@ Axel Amthor

My Jquery file code is as:

   $(document).ready(function() {

   $('#submit').click(function() {
        submit_fxn(); 
        $('#form_nuser').submit(function(e) {
            return false;
        });
});

 function submit_fxn(){

    var check_flag = 0;
    var lgn_usr = '<?php  echo $local_session; ?>';
        alert(lgn_usr);

};


});
Partap
  • 181
  • 1
  • 9
0

OKay, I got it. $_SESSION is a PHP array, we cannot set it on the client side via Javascript. The value has to be sent to the server to be stored in the array. Now, I am going for other method....

Partap
  • 181
  • 1
  • 9