First, here are the hierarchy of files:
首先,这里是文件的层次结构:
system/
...index.php
...core/
.......MasterView.php
.......Test.php
...sources/
.......ajax/
............ajaxtest.php
.......js/
............jstest.js
and so on.
index.php includes Test.php.
索引。php包含Test.php。
Test.php contains these lines:
测试。php包含这些线:
$GLOBALS['foo'] = 'foo'; // note 1
require(ROOT.'/core/MasterView.php'); // render master view.
MasterView.php contains simple html tags but calls on jstest.js in system/sources/js/ directory.
MasterView。php包含简单的html标记,但是调用jstest。js在系统/资源/ js /目录中。
jstest.js in system/sources/js/ directory made an ajax call to ajaxtest.php.
jst。在system/sources/js/ directory中的js对ajaxtest.php进行了ajax调用。
ajaxtest.php in system/sources/ajax/ directory contains this line:
ajaxtest。系统/源/ajax/目录中的php包含以下一行:
echo $GLOBALS['foo']; // note 2
After running index.php on browser, the following error occurs:
跑后指数。php浏览器出现以下错误:
Undefined index: foo in ...ajax\ajaxtest.php ...
particularly pointing to note 2
line. My question is, why does php does not recognize foo
index when I have defined it on note 1
line before calling MasterView.php
?
特别指向注意2线。我的问题是,当我在调用MasterView.php之前在注释1行定义了foo索引时,为什么php不识别foo索引?
PS: I know the above method is not the best way to do it but I only give it as an illustration to my problem.
我知道上面的方法并不是最好的方法,但我只给出一个例子来说明我的问题。
EDIT:
编辑:
- I've tried using
$_SESSION['foo']
instead of$GLOBALS['foo']
in both files just to mention one solution I've tried. Same error occurs. Php does not recognize the indexfoo
. - 我尝试在两个文件中使用$_SESSION['foo']而不是$GLOBALS['foo'],只是想提一下我尝试过的一种解决方案。同样的错误发生。Php不识别索引foo。
1 个解决方案
#1
4
jstest.js in system/sources/js/ directory made an ajax call to ajaxtest.php.
jst。在system/sources/js/ directory中的js对ajaxtest.php进行了ajax调用。
Global variables are global to the same request only. If you connect files with an AJAX request in between, the global variable is not available because AJAX will create a new request.
全局变量仅对同一个请求是全局的。如果您将文件与AJAX请求连接在一起,那么全局变量是不可用的,因为AJAX将创建一个新的请求。
You can share data across multiple requests by creating some sort of Server Session State by using cookies, a database or PHP sessionsDocs.
你可以共享数据跨多个请求通过创建某种服务器会话状态通过使用cookie,数据库或PHP会话文档。
#1
4
jstest.js in system/sources/js/ directory made an ajax call to ajaxtest.php.
jst。在system/sources/js/ directory中的js对ajaxtest.php进行了ajax调用。
Global variables are global to the same request only. If you connect files with an AJAX request in between, the global variable is not available because AJAX will create a new request.
全局变量仅对同一个请求是全局的。如果您将文件与AJAX请求连接在一起,那么全局变量是不可用的,因为AJAX将创建一个新的请求。
You can share data across multiple requests by creating some sort of Server Session State by using cookies, a database or PHP sessionsDocs.
你可以共享数据跨多个请求通过创建某种服务器会话状态通过使用cookie,数据库或PHP会话文档。