Solved

Javascript/PHP Timer?

Posted on 2011-03-06
3
245 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:newmie22
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
ID: 35050556

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
 
LVL 6

Assisted Solution

by:GregArnott
GregArnott earned 250 total points
ID: 35050673
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
 

Author Closing Comment

by:newmie22
ID: 35050998
Great, thank you both for your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now