dtt2012 2017-05-27 16:39
浏览 47
已采纳

JQuery load()不会识别pre php包含

I'm having a page which loads content using the load() method from jQuery.

Here is how it looks like (my main page) :

<?php include 'inc/header.php' ; ?>
Some Content
<button onclick="$('#loadContainer').load('contentToLoad.php')">Load</button>
<div id="loadContainer">
</div>
<?php include 'inc/footer.php'; ?>

Then I have my contentToLoad.php which is a php module, using sessions, functions from files ect ...

In the header.php, I have my head tags, a session_start(), and all the necessary files for the good working of contentToLoad.php included.

But it seems that my contentToLoad.php doesn't manage to reach the files and the declarations of the header.php.

Do I really need to rewrite all of these at the top of my contentToLoad.php ? Or is there a solution to make contentToLoad.php recognize the code from header.php ?

Thanks

  • 写回答

2条回答 默认 最新

  • dousheyan0375 2017-05-27 17:17
    关注

    You're misunderstanding the way PHP and JS interact, so to answer your question I'll just explain that and you should figure out the answer yourself.

    PHP code always runs before the page loads (with some very small exceptions that we don't really care about), or to make it simple, "while the page is white". After PHP finishes executing, the output it sent and shown to the client.

    Now what you're trying to do is run a script after the page has already loaded, by using javascript, this is fine, but the server doesn't see any difference between this and just loading the index, and then in a new tab opening contenToLoad.php a few seconds later, all you're doing is instructing the browser via javascript to load the information inside your div.

    The two scripts are being ran at different times, in different processes, as different instances, and have no idea about the existence of the other script. This is very different from including one PHP script inside another, which is on a very basic level the same as copy pasting that script where you're including it, therefore they obviously run at the same time and your included script can access whatever information the host script has declared.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程