将主页之间的变量转移到内部页面

时间:2015-12-18 11:29:59

标签: php jquery html

在分离html和php代码的方法中,我提出了以下结构 - Index.php - 代码的大脑 - 接收数据,处理数据,然后选择发送给用户的页面。

在访问主站点之前,您必须登录或注册。执行此操作后,index.php将加载将具有dyn_content块的site.html - 内容将由javascript加载,具体取决于用户点击的链接。

的index.php

...
case "inside": {
            site();
            break;
        }
...
function site () {
    $Page = file_get_contents("site.html");
    echo $Page;  // if it is not good way to display page, plz let me know the better way.
    return 1;   
}
... 

site.html

...
<div id="main_page">

            <div id="dyn_content">

            </div>
        </div>
...

jquery的

...
$(document).on("click",'#link_profile',  function (){   
        $("#dyn_content").load("in_page/profile.php",function() {
            $('#m_main_top').load("menu/m_profile.php");});
            return false;
    });
$(document).on("click",'#link_setings',  function (){   
        $("#dyn_content").load("in_page/set.php",function() {
            $('#m_main_top').load("menu/m_set.php");});
            return false;
    });
...

要在din_content中加载页面时显示数据,我需要从数据库中获取数据(例如,profile.php的用户数据)。由于我分离了代码,我需要在index.php中完成(我不能在profile.php中执行它,因为它会破坏分离的概念)。但是,index.php中的变量在dyn_content中加载的页面中不可见(但我希望它们能够工作,因为我们仍然在index.php中)。

所以第一个问题 - 是否可以在块dyn_content中加载的页面中显示索引变量?可能是问题是页面site.html已经被index.php加载,并且当jquery加载的页面(dyn_content块中的页面)由于此操作发生在用户端时太晚了?

所以我提出了想法 - 在某种空间中获取所有需求数据,然后在需要时获取这些数据。所以我提出了以下想法:

  1. 我想过将变量从index.php传输到jquery,然后从jquery传递到dyn_block页面。但我认为这不是最好的方式。
  2. 将index.php中的json数据发送到其他页面。它看起来不错,但问题是我们不知道目的地,直到用户点击其中一个链接(它可以是settings.php或profile.php或其他链接)。此外,只有在需要显示页面时才能处理数据。所以我认识到这个想法并且我不确定它是最好的。
  3. 使用会话或浏览器的缓存。我认为,这是如何做到这一点的最佳方式。但我不确定它是否合适,因为我们可以使用50个变量来吸引用户。如果用户端已满,我们将无法显示数据。
  4. 所有想法都是我得到的。 所以问题是 - 如上所述,我可以使index.php中的变量在内部部件中可见吗?如果不是,转移它们有多好?

1 个答案:

答案 0 :(得分:0)

有更多统一和简化的方法:
假设你在index.php中有初始的html部分:

// index.php content
...
<nav>
    <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="index.php?page=profile">Profile</a></li>
        <li><a href="index.php?page=settings">Settings</a></li>
        <li><a href="index.php?page=other">Contact Us</a></li>
    </ul>
</nav>
...

...
<div id="main_page">
    <div id="dyn_content">

    if(isset($_GET['page']) && !empty($_GET['page'])){
       switch($_GET['page']){
           case "profile":
               $some_variable = 111;
               $profile_id = $_SESSION['user_id']; // just for example
               include ("in_page/profile.php");
               break;
           case "settings":
               $some_variable = 222;
               include ("in_page/set.php");
               break;
           default:
               include ("index.php");

       }
    }

    </div>
</div>

...

从任何包含的页面内部,您可以访问在包含

之前声明的“父”页面的全局变量
// profile.php content
<a href="/users/<?php echo $profile_id ?>">My link</a></br>
<img src="/users/images/<?php echo $profile_id ?>"/> // avatar
... // approximate content