<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">

</body>
</html>

here is java script

<script>
var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;

}

function sayTime() {
  finalTime = time / 10;
  //return finalTime;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
}
</script>

How to save finalTime in php variable like $time??

Recommended Answers

All 9 Replies

I don't believe you can do this without a page load. You could pass the variable via the URL and then use it in php with $_GET. You could also pass it to your script with Ajax. It really depends on what you are trying to accomplish.

If you want to use the $time variable somewhere else in the same page, you would be better off getting the time with PHP and then printing it, rather than using the javascript.

hi death_oclock,
can you post your example code??

Oh wow I completely missed the point of your code. Um you can't find out when the user left the page using PHP, not that I know of at least. I think you would have to use Jaseva's method of passing it to the next page through a form of some sort.

Hi..
i got the idea ...
i am transferring variable through cookie
and got result...

But suppose when cookies are disable what will be the alternative to transfer variable from JavaScript to php??

Have an invisible <iframe> in your page somewhere, have sayTime() refresh the iframe to a page that will take the time as a get variable in the url. The invisible page can then store the time in a database.

Have an invisible <iframe> in your page somewhere, have sayTime() refresh the iframe to a page that will take the time as a get variable in the url. The invisible page can then store the time in a database.

Can you explain this to me with example??

Oh wow sorry, I'm not great at javascript so I was doing my usual bizarre, roundabout way of doing things. Just use ajax!
Try this:

<script>
var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;

}

// function mostly from w3schools.com
function sendTime(t)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return;
      }
    }
  }

  xmlHttp.open("GET","time.php?t=" + t,true);
  xmlHttp.send(null);
}

function sayTime() {
  var xmlHttp;
  finalTime = time / 10;
  //return finalTime;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
  sendTime(finalTime);
}
</script>

A side note, isn't it wonderful how complicated Internet Explorer makes everything? :\
For more info on AJAX, try w3schools: http://w3schools.com/ajax/default.asp

Member Avatar for diafol

Have you thought about the Prototype library? It makes short work of the ajax bit. You can get this from Prototype themselves or as part of the Scriptaculous framework. For some weird reason, the Scriptaculous version tends to be more up-to-date.

<script>
var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;

}

function sayTime(){
  finalTime = time / 10;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
  var url = "time.php";
  var oOptions = {method: "post", parameters: "t=" + finalTime};
  var oUpdate = new Ajax.Request(url,oOptions);

}
</script>

This means you don't need the extra function (sendTime). I have to admit I'm a bit of a newbie to JS, so if I've misunderstood your app, apologies. Note that I've used the POST method to send data, you can use GET if you prefer.

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.