javascript - JQuery load() won't recognize pre php inclusions

547

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

750

Answer

Solution:

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.

949

Answer

Solution:

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 insidecontenToLoad.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 insidecontentToLoad.php toinclude_once( __DIR__ .'/include_file.php');

People are also looking for solutions to the problem: Having issue with define scopes while trying to write by google spreadsheet API v4 in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.