0

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

Cephou
  • 257
  • 5
  • 23
  • Check your apache/server error log, and look at the response in Inspector > Network tab to see if the page returns a 500 error. Unless it's a POST, you could also open that request in another tab. Your problem is on the server in the header, with the PHP, not the browser, so you need to look there. – Jared Farrish May 27 '17 at 17:27
  • Try this: https://stackoverflow.com/a/13200206/451969 and for grins you might go through Symfony's [Create Your Own Framework guide](http://symfony.com/doc/current/create_framework/http_foundation.html) that includes morphing a very basic one-page app into a fully bootstrapped application. Worth reviewing for ideas. – Jared Farrish May 27 '17 at 17:48

2 Answers2

1

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');

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • So that means I have to reincludes the files from the header.php in the contentToLoad.php ? – Cephou May 27 '17 at 16:59
  • No no just the include the files inside the `contentToLoad.php` if you have more includes inside it so change to `include_once( __DIR__ .'/include_filename.php');`, also I fixed your header is was having a blank space in the include I change it in the example code. – Otávio Barreto May 27 '17 at 17:01
  • And so I have to rewrite de session_start() ? I also don't want to move out the files from the header.php because it's used somewhere else :( – Cephou May 27 '17 at 17:07
  • No just let is as it is, change your code to the example I made and see if it works , it should work. just tell me if it's working – Otávio Barreto May 27 '17 at 17:10
  • It is not :( It can't recognize the session. In the contentToLoad.php I have 0 includes, it all relies on the includes made in header.php. Changing the inclusion of the header.php to include_once in my main file didn't solved the problem. – Cephou May 27 '17 at 17:13
  • you probally missing a ?> in your session , please edit the question and show the session file, It would be better to see where is the problem if you show the code all files `inc/header.php` , `contentToLoad.php` and `inc/footer.php` – Otávio Barreto May 27 '17 at 17:14
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145278/discussion-between-otavio-barreto-and-cephou). – Otávio Barreto May 27 '17 at 17:38
1

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.

Cârnăciov
  • 1,169
  • 1
  • 12
  • 24
  • Thanks for that explanation ! What could be the solutions to solve this ? Do I have to rewrite the files inclusion or is something else possible ? – Cephou May 27 '17 at 17:24
  • `contentToLoad.php`(or any script you wish to execute and load via js) needs to be able to run if you visit http://yourwebsite.com/contentToLoad.php as well, so yes, you need to re-include everything. If there's also code that you need to copy/paste (for example, get session data, validate user, store user information in an array) I would recommend moving those to another file and including said file where you need it, duplicate code is very bad. – Cârnăciov May 27 '17 at 17:30
  • @JaredFarrish what I understood from his question is that he's trying to access functions and values declared inside `header.php` from `contentToLoad.php`, which like I said might seem possible (as you can do this if you include the files one after another) but is not since he's loading it at a later time via jquery. I might be wrong, but I think the other answer is the misleading one as it refers to relative paths which is not the case. – Cârnăciov May 27 '17 at 17:40
  • Ok, fair enough. I still say Occam's razor. See the file first. – Jared Farrish May 27 '17 at 17:42
  • I'm not making assumptions though, I'm interpreting the literal meaning of his question after reading it thoroughly a couple of times. He first describes having some stuff declared in `header.php`, such as fetching some session data. He then says `...contentToLoad.php doesn't manage to reach the files and the declarations of the header.php.` which without further explanation would mean that he's using the wrong paths when including. However the `Do I really need to rewrite all of these at the top of my contentToLoad.php ?` sounds to me like re-fetching the session data, not fixing the paths. – Cârnăciov May 27 '17 at 17:52
  • 1
    That last statement can also be interpreted as copy and paste, "why doesn't it work I don't know how to debug brute force it". It doesn't mean that would solve the problem if the `header.php` include is actually there. (I don't know? Right.) That was how I interpreted it. Rereading it, I see what you're suggesting fits the question too. Without seeing the _file_, it's still conjecture. Very simple to clear up, even with the server error log (functions aren't declared). – Jared Farrish May 27 '17 at 17:55
  • @JaredFarrish check out https://stackoverflow.com/questions/44219276/jquery-load-wont-recognize-pre-php-inclusions/44219666#comment75449809_44219429 – Cârnăciov May 27 '17 at 17:59
  • 1
    Ahh, very good. I would give you an upvote if I hadn't already. `;)` – Jared Farrish May 27 '17 at 18:00
  • As a @aron9forever said, the problem is not the inclusion of the header.php or inclusion of the files useful for the good execution of contentToLoad.php. The problem is the use of the method load() which doesn't allow contentToLoad.php to access the tools it need declared in the header.php :) – Cephou May 27 '17 at 18:32