Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript/PHP Timer?

Posted on 2011-03-06
3
Medium Priority
?
255 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

926 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