How to count number of rows from database after every 2 seconds without refreshing page

" id sender receiver message " these are database fields

I want to count and display number of message received after every 2 seconds, like

OUTPUT:- " Messages (1) "

Try AJAX. It will not refresh the webpage, though it will send a separate request to the server asking for the information you need.

function showRecords(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("messagesDiv").innerHTML="Messages (" . xmlhttp.responseText . ")";
    }
  }
xmlhttp.open("GET","getrecords.php",true);
xmlhttp.send();
}

so getrecords.php contains the php code that queries the database for new records, and you call the javascript unction every 2 seconds. document.getElementById["messagesDiv"] refers to an html container with that id, which reflects the quantity of records in the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.