tags:

views:

33

answers:

3

There is a php file (current.php) with some variables, like:

function do() {
    $var = 'something';
}

And one more php file (retrieve.php), which is loaded to current.php with jQuery ajax .load().

The problem is - retrieve.php doesn't see $var.

Tryed this (inside retrieve.php, shows nothing):

global $var;
echo $var;

How to fix?

Thanks.

+1  A: 

The problem is - retrieve.php doesn't see $var.

Sure it is!
all current.php variables are long dead along with current.php itself, which was run, print some HTML and die.

you have to pass required value using standard HTTP mechanisms. you know - GET, POST etc.

Col. Shrapnel
You could also put it into the users session.
Treffynnon
+3  A: 

Some things you must be aware of:

  • When you use PHP, you do not download a file: you execute a script and retrieve its output.
  • PHP variables are destroyed when the script ends. You cannot share variables between two scripts unless you store them somewhere (e.g., in a database or session file).
  • PHP variables are local to the function where you define them, unless you issue a global $foo; statement inside the function.
  • jQuery is a JavaScript library. JavaScript and PHP are different languages: they cannot see each other variables.

Said that, I suggest you reconsider your question and try to explain what you need to accomplish rather that how you want to implement it.

Álvaro G. Vicario
+1  A: 

If you want the script you load through AJAX get the value of the vars from the page initiating the AJAX load then you either have to pass the values when loading the AJAX script or store them somewhere temporarily (in DB linked by session ID, or in a session var) so you can retrieve them easily.

wimvds
$var contains a big array, seems it better to use global $var
Happy
A global var will not be available between 2 different requests (which is the case here since you load the 2nd page through AJAX). A global var will only help if you include one page into the other using include(_once) or require(_once) in PHP.
wimvds