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 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱