How do I load a php include into a page with ajax whilst the page is loading

31,902

Solution 1

If you're using jQuery, you could you use one of their ajax calls to load your html from include.php. For example you could use the load() function.

For example:

<div id="content"></div>

<script>
$(document).ready(function() {
    $("#content").load("/yourpath/include.php");
});
</script>

Solution 2

use jquery , load php after DOM ready (before they render)

<div id="include"></div>
$(function(){
    $("#include").load("include.php");
});
Share:
31,902
Ben Paton
Author by

Ben Paton

Web designer and developer from the UK. Work on a mixture of work web projects and my own web projects. Self taught in PHP among other things so bear with me.

Updated on June 05, 2020

Comments

  • Ben Paton
    Ben Paton almost 4 years

    I have a php include which takes awhile to load because PHP has to fetch a lot of data. I don't want to slow the whole webpage loading waiting for this include so how can I load this one include with ajax? I don't want the ajax to be triggered by a button click I just want it to load up the include as the page is loading so that if you look at my example below 'Some more html content' would be displayed whilst the inlude.php is still loading in.

    <html>
    <head>
    </head>
    <body>
        Some html content
        <script>
            Ajax load 'include.php';
         </script>
        Some more html content
    </body>
    </html>