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

4
  • And your JS file is included on the top or bottom of the page ? Commented Jan 20, 2014 at 11:11
  • Please consider looking at How to put php inside javascript? or Include PHP inside javascript (.js) file. Commented Jan 20, 2014 at 11:11
  • jQuery is NOT A LANGUAGE! jQuery is a framework written in javascript! Commented Jan 20, 2014 at 11:12
  • @ Axel Amthor, we know this thing that Jqeury is not a language.. It is a framework Commented Jan 20, 2014 at 11:21

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

This can break very easily depending on the value of the variable. Correct is sessionVar = <?= json_encode(...) ?>; -- note no explicit quotes around the value.
@ Axel Amthor I have done the same thing in JQuery Code as var lgn_usr = <?php echo json_encode($_SESSION["sessionusername"]) ?>; alert(lgn_usr); But in alert it displays as <?php echo json_encode($_SESSION["sessionusername"]) ?>;
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: <?php echo
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);
?>

Comments

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

5 Comments

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).
I assume that this var lgn_usr = '<?php echo $_SESSION["sessionusername"] ?>'; 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.
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
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. 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
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);

};


});

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.