简体   繁体   中英

How to store PHP session variable in JQuery variable?

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

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

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.

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);
?>

@ 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

@ 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);

};


});

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

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