1

I have this jquery function

function updateStatus(){
      $.getJSON("<?php echo $_SESSION['filnavn']; ?>", function(data){

                           var items = [];

                           pbvalue = 0;

                           if(data){

                                var total = data['total'];
                                var current = data['current'];
                                var pbvalue = Math.floor((current / total) * 100);

                                if(pbvalue>0){

                                    $("#progressbar").progressbar({

                                        value:pbvalue
                                    });
                                }
                            }
                            if(pbvalue < 100){

                               t = setTimeout("updateStatus()", 500);
                            }
      });
}

Is it possible to get the JSON from a PHP session variable instead of a json file?

As I have understood I can get the session data from the session like this:

//json test
var jsonstr = $_SESSION['json_status'];
//parse json
var data = JSON.parse(jsonstr);

But I do not know how I can do that with out the getJSON function?

1
  • Not a solution, but don't ever pass strings to setTimeout. It uses eval! Pass a function: t = setTimeout(updateStatus, 500); Commented Sep 6, 2013 at 18:50

2 Answers 2

1

You're reading too much into it. .getjson is just a $.ajax() call that EXPECTS to get a json reponse from the server. That's all.

It doesn't matter WHERE PHP gets data from, as long as it spits out json text.

Whether that json text was just retrieved from a file/db, or dynamically generated with json_encode(), as long as the browser receives json text, things will "work".

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

Comments

0

Your best bet here is to create a php file that can act as the target of getJSON that returns the json from your session.

 <?php
 session_start();
 if (isset($_SESSION["filnavn"])){
     echo $_SESSION["filnavn"];
     // Or, if the key contains an object instead of a json string, use
     // echo json_encode($_SESSION["filavn"]);
 } else {
     // echo the json you want here if the session variable is not set 
     echo "{}";
 }
 ?>

Then in your jquery code change the getJSON to this

  $.getJSON("/path/to/php/file.php", function(data){...});

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.