Javascript/PHP Timer?

I have a an online learning module that is essentially different questions, one per page that the user answers and then when they submit the answer the php pass through page sends their answer to the database where their userid is stored.

I need to time how long it takes the user to answer each question, so basically how long they are on each page, and i need those results sent to the database, there is a field for each of the 10 questions, ques1time, ques2time..etc.

I found this script online which gets the time but displays it to the user.  I don't want them to see, is there a way to instead pass the time to the php page and have it sent to the database.  I'm open to any suggestions to achieve the results i'm looking for, i'm just new at code and experiencing some trial and error.
<!-- Paste this code into an external JavaScript file named: timePage.js  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Cody Shaffer :: http://codytheking313.byethost11.com */

var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
/* Remove the "//" below to display in the title bar
  the amount of time the visitor has been on the site.
  Be aware though, that it does tend to be a bit distracting. */
// document.title = finalTime+" seconds you been here for!";
}

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



<!-- Paste this code into the HEAD section of your HTML document.
     You may need to change the path of the file.  -->

<script type="text/javascript" src="timePage.js"></script>



<!-- Paste this code into the BODY tag -->

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

Open in new window

newmie22Asked:
Who is Participating?
 
sjklein42Commented:

Open in new window

To do this you could fill in a hidden input field in your form with the elapsed time, using javascript.  Time passed is in milliseconds.  Divide by 1000 to get seconds.  Add the hidden input field to your form.  This form is just for testing.



<script>
var currentTime = new Date();
var startTime = currentTime.getTime();

function getElapsedTime()
{
	var endTime = new Date();
	var elapsedTime = (endTime.getTime() - startTime);
	form1.elapsedTime.value = elapsedTime;
}
</script>



<form id=form1 target=_blank>
<input type=hidden name=elapsedTime>
<input type=submit onclick="getElapsedTime()">
</form>

Open in new window

0
 
GregArnottCommented:
Fast alteration:
  var field = document.getElementById("questtime");

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
  field.value = finalTime;
}

Open in new window

and in the body of the page have the html for a hidden input named questtime.
ie:
<input type="hidden" name="questtime" id="questtime" />

Open in new window


Remove onunload="sayTime()" from the body, and the sayTime() function as well as they'll no longer be needed.

The hidden field will now have the time in seconds updated every second hidden from view, and will submit that value as one hits submit to leave the page.
0
 
newmie22Author Commented:
Great, thank you both for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.