Web Owl
Web Owl

Reputation: 569

ajax: updating without refreshing

http://pastebin.com/bnMaxjep

http://www.vidyasocks.com/index.php

See the ticker at the bottom, I want to update that so it shows without having the users refresh the page. I am using ajax to do it.

How do I do it? This is my first time using it.

Upvotes: 1

Views: 376

Answers (1)

Event_Horizon
Event_Horizon

Reputation: 707

The main page would go something like this:

<?php session_start(); 

$_SESSION['visitcount']=0;

?>
<script>
function runcounter()
{
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("counterelement").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","counter.php",true);
//may need these:
var params="id=testdata";
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
//need rest
xmlhttp.send();

setTimeout("runcounter()",1000); //refresh function every second

}
</script>
<body onload="runcounter()" >

<div id="counterelement">0</div>

This would update the function (counter) every second and run once on page load as well.

PHP:

session_start();
$_SESSION['visitcount']++;
echo $_SESSION['visitcount'];

Upvotes: 2

Related Questions