How to automate html content update?

I am trying to create an FB news feed, but to display content from a database. My code already reads the number of rows in the table, but it needs to refresh the page before updating the html content.

Here is my code:

part of my html file:

<section id="reportcontent"></section>
<script>
    (function refreshNews()
    {
        $("#reportcontent").load("php/ajax.php");
    })();
    setInterval('refreshNews()',1000); 
</script>

content ajax.php

<?php 
    require("database.php");

    $sql = "SELECT * FROM report";

    $res = odbc_exec($conn,$sql);

    while($feedItem = odbc_fetch_array($res))
    {
        echo "hello! <br>";
    }
?>

As you can see, each row of my table will display welcome text. But in order to view the new number of rows, I need to refresh the page. Thanks for the help!

+4
source share
1 answer

. setInterval, ''. , ​​

setInterval('refreshNews',1000); 

, , PHP- .
.

. Ajax. , , , , . setTimeout AJAX.load()

function refreshNews()
    {
       $("#reportcontent").load("php/ajax.php", function(){setTimeout(refreshNews, 1000);});
        
    }();


window.onload= function(){
    refreshNews(); 
  }
<section id="reportcontent"></section>
Hide result
+2

Source: https://habr.com/ru/post/1613936/


All Articles