HIRA THAKUR
HIRA THAKUR

Reputation: 17757

response data to $_SESSION?

I am pushing some data in to my javascript array every 30 seconds.This data comes comes via ajax response.I want to use this array on the next page I visit without its content being lost.

setInterval(function () {
      $.ajax({
                url: "../controller/controller_session.php",
                type: 'POST',
                data: {data to send},
                dataType: "json",
                success: function(data) {

                     ARRAY.push(JSON.stringify(data))
        },30000)
    } 

How do I transport this data using $_SESSION to some other page on website and how do I retrieve it back and reuse it my javascript.

Upvotes: 1

Views: 61

Answers (2)

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

Javascript cant read your session data, that is stored on the server and Javascript only manipulates the actual data what you have. You would have to make an AJAX call to another PHP page to get the $_SESSION and then send the data back to the AJAX success function.

On a PHP file have

session_start();
if(isset($_SESSION['value'])){
    $session = $_SESSION['value'];
}

echo isset($session) ? $session : '';

Then you call that page in an AJAX call and check to see if the page returned something.

EDIT:

success: function(data){
    $('.div').html(data);
}

Upvotes: 1

Sudip
Sudip

Reputation: 2051

Set the SESSION variable on controller_session.php page just before the return. Save the result as a string in your SESSION variable.

On the next page use the SESSION value (I guess it's a JSON string) parse it and use it in your JavaScript code.

Upvotes: 0

Related Questions