0

I have a php file where I call different php functions to render a page. I am wanting to refresh portions of the page every 5 mins by calling the php functions again. My site works fine as it is but I am not able to update the content without reloading the entire page.

php file 1 index.php

<?php
    //  Enable functionality
    require_once ("lib/config.php"); //this file has all my php includes and db configurations

    site_head();

    front_stats();

    front_browser(20);

    site_foot();
?>

php file 2 which includes the functions above - layout.php

<?php
    function front_stats ()
    {
        //my content thats rendered on the page
    }

    function front_browser ($per_page)
    {
        //my content thats rendered on the page
    }

    etc.
?>```
4
  • When the function gets called again you want to rerender the page? Commented Aug 16, 2019 at 2:16
  • What about if user is reading the content of rerender portion and then get refreshed . What will user feel ... Commented Aug 16, 2019 at 2:18
  • @cyptochat Research "php update ajax" for more information :) Commented Aug 16, 2019 at 5:03
  • @MosesSchwartz I want to only rerender two of the functions front_stats and front_browser. There is alot of php and html in both of those functions so I was thinking I should just be able to call the function again so the data is updated on the page. Commented Aug 16, 2019 at 23:57

1 Answer 1

2

Set the sections you want to reload into special tags i.E

<div id=reload></div>

then setup a new .php file that will return only the html code that should be replaced into the reloaded section.

Now include a Javascript file that accesses this php file via an ajax request periodically.
Inside of the ajax request itself you'll need to fetch the new content and overwrite the old content. Here is an example for the ajax and replacing part:

xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=function({
    if(this.readyState==4&&this.status==200{
        document.getElementById("reload").innerHTML=this.responseText
    }
};
xmlhttp.open("GET","yournewphppagename.php",true);
xmlhttp.send();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.