最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get session variables from php server with Ajax function? (PHP HTML JS Ajax) - Stack Overflow

programmeradmin3浏览0评论

so in my php I have something like this

$_SESSION['opened'] = true;

But It will not be set to true until user will perform some actions with some other html\php pages

So I need on some Ajax function to be able get this session variable. And some PHP sample of function to get variable in form ready for Ajax to get it.

so I need something to AJAX requesting to an action (to some simple php code) which will return a value from $_SESSION.

How to do such thing?

so in my php I have something like this

$_SESSION['opened'] = true;

But It will not be set to true until user will perform some actions with some other html\php pages

So I need on some Ajax function to be able get this session variable. And some PHP sample of function to get variable in form ready for Ajax to get it.

so I need something to AJAX requesting to an action (to some simple php code) which will return a value from $_SESSION.

How to do such thing?

Share Improve this question edited Jan 24, 2011 at 7:45 Haim Evgi 125k46 gold badges221 silver badges226 bronze badges asked May 4, 2010 at 12:25 RellaRella 66.9k112 gold badges373 silver badges642 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

Simple jQuery example:

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', function (data) {
    session = data;
});

And getsession.php:

<?php
session_start();
print json_encode($_SESSION);

You're not required to use jQuery for AJAX, but I highly recommend it.

Edit:

In response to:

I want to be able to tell my JS function what variable I want to get.

You can try this (untested):

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', {requested: 'foo'}, function (data) {
    session = data;
});

And the PHP:

<?php
session_start();
if (isset($_GET['requested'])) {
    // return requested value
    print $_SESSION[$_GET['requested']];
} else {
    // nothing requested, so return all values
    print json_encode($_SESSION);
}

Have a look at the $.get documentation for a more detailed overview.

PHP File at http://my.host/response.php

<?php
session_start();
if(isset($_SESSION['opened']))
    echo "true";
?>

Then in HTML, Add jQuery such as:

<script type="text/javascript" src="/path/to/jQuery-x.y.z.js"></script>

Then:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url:'/response.php',
            cache:false,
            success:function(data){
                // Do something with the result
                if(data=="true"){
                    $('#mydiv').show();
                }else{
                    $('#mydiv').hide();
                }
            }
        );
     });
</script>

And add to myform.php:

<h1>Some Random HTML</h1>
<div id='mydiv' class="<?php if(isset($_SESSION['opened']) && $_SESSION['opened']) echo "hidden_class";?>">
 ...</div>

As this will give a consistent experience to those without JavaScript. You don't have to be showing/hiding a div. You could do anything really.

发布评论

评论列表(0)

  1. 暂无评论