0

我遇到了一个问题,即我在外部 php 脚本中声明的变量没有显示在稍后通过 jquery 加载的页面的一部分中。

简化代码:

索引.php:

<?php
require_once "init.php";
?>
//the rest of the page is added here.

初始化文件

<?php
$test = 'foo';
?>

主页.php

<?php
echo $test;
?>

单击 div 时,jquery 将使用 .load 将 'home.php' 添加到页面中。

问题是 $test var 没有显示,因为页面上没有使用 'home.php' 运行 'init.php'。有什么办法可以解决这个问题吗?

谢谢

4

2 回答 2

2

我想唯一的解决方案是在 home.php 中包含 init.php

否则变量将是未定义的。

于 2012-04-15T18:21:25.537 回答
0

当 Jquery 请求 home.php 文件 $test 时不再定义,因为 index.php 代码已经完成运行并将结果发送到客户端。 如果您希望在页面运行后将变量存储在服务器上,您可能需要查看Sessions 。

于 2012-04-15T18:25:00.887 回答