Reputation: 297
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
Upvotes: 0
Views: 54
Reputation: 1144
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.
Upvotes: 1
Reputation: 1558
Jquery load only load the html you should include the php it self like
<?php
include_once( __DIR__ .'/contentToLoad.php');
?>
but if you only need the result of the php page so, try to change your includes inside contenToLoad.php
to work with __DIR__
there is a difference between relative and includes path so it should work for you.
example:
<?php include_once ("inc/header.php"); ?>
Some Content
<button onclick="$('#loadContainer').load('contentToLoad.php')">Load</button>
<div id="loadContainer">
</div>
<?php include_once ("inc/footer.php"); ?>
change all includes inside contentToLoad.php
to include_once( __DIR__ .'/include_file.php');
Upvotes: 1