When firing multiple ajax requests using the setinterval()
function, I noticed that both requests are bringing back the same information from another page. Here is the JavaScript code:
function views()
{
setInterval(function(){var xmllhttp
//alert("views")
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("latestviews").innerHTML=xmlhttp.responseText
}
}
xmlhttp.open("GET","latestviews.php")
xmlhttp.send()},5000);
}
function recentposts()
{
setInterval(function()
{
var xmllhttp
//alert("recent")
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("latest").innerHTML=xmlhttp.responseText
}
}
xmlhttp.open("GET","recent.php")
xmlhttp.send()},5000);
}
And here is the HTML code:
<body onload="views(),recentposts()">
<div class="latest" id="latest">
<span class="latestin">
<label class="label"><i>Recent Post</i></label>
</span>
<?php
$con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
$result=mysqli_query($con1,"SELECT articleid,title FROM article order by articleid desc LIMIT 6");
$divid=0;
while($row = mysqli_fetch_array($result))
{
$id=0;
$id=$row['articleid'];
echo"<div class='recent' onclick='ajaxinput($id)' id=$id style='cursor:pointer;'>";
echo $row['title']."<br>";
echo"</div>";
}
mysqli_close($con1);
?>
</div>
<div class="latestviews" id="latestviews">
<span class="latestin">
<label class="label"><i>Top viewed Post</i></label>
</span>
<?php
$con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
$result=mysqli_query($con1,"SELECT articleid,title FROM article order by views desc LIMIT 6");
$divid=0;
while($row = mysqli_fetch_array($result))
{
$idd=0;
$idd=$row['articleid'];
echo"<div class='recent' onclick='ajaxinput($idd)' id=$idd style='cursor:pointer;'>";
echo $row['title']."<br>";
echo"</div>";
}
mysqli_close($con1);
?>
</div>
</div>
</body>