Home > Software engineering >  Loading data from database in every few seconds without page refresh
Loading data from database in every few seconds without page refresh

Time:09-24

I want to load contents from database in every 10 seconds to a particular . But however i try whole page gets refreshed. Not getting where oi am going wrong.

Here is my code

<?php 
include('header.php');
include('includes/Connection.php');
include('includes/GeneralFunctions.php');
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
setInterval(function(){
      $('#test').load('#test');
 },5000);
</script>


<div id="test">
<?php
$l1 = $con->prepare("SELECT msg, topic FROM messages ORDER BY id DESC");
$l1->execute();
$cntmsg = $l1->rowCount();
foreach($l1 as $r1)
{
    echo $r1['topic']."<br />";
    echo $r1['msg']."\n";
    
}
?>
</div>

CodePudding user response:

Use ajax in your SetInterval function. ajax() method is used to perform an AJAX (asynchronous HTTP) request

$.ajax({url: "demo_test.txt", success: function(result){
    $("#div1").html(result);
  }});
  •  Tags:  
  • php
  • Related