Jump to content

Recommended Posts

Hello everyone,

 

I paid to get the following script developed because I'm not that great with JavaScript, the script does not seem to work for users using IE7/8 on Win XP.

 

What the script is built to do is countdown X seconds and activate a continue button.  The script uses Ajax and PHP to save the logged in user to the database so if they leave the page and come back a few days later the countdown timer will start back where they left off.  This script is used for a quiz page, and only logged in users can access it.

 

Here is my page's HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var userid = "1"; // User ID
var quizid = "15"; // Quiz ID
var segment = "1"; // Quiz Segment
var defaultTime = 3300; // Counter Time In Seconds
var updateIntervals = 30000; // Update Database Intervals In Miliseconds
</script>
<script type="text/javascript" src="components/com_quiz/js/quizTime.js"></script>

HERE IS quizTime.js

$(document).ready(function()
{

$.post("components/com_quiz/quizTime.php",{action:"getTime",userID:userid,quizID:quizid,defTime:defaultTime,segmentNum:segment,rand:Math.random()},function(returnTime)
{	
	time=parseInt(returnTime);
		currentTime=time;
		countDown(time);

});
});	


//CONVERT SECONDS TO DISPLAY FORMAT	
function countDown(totalSeconds)
{
totalSeconds=parseInt(totalSeconds);
hours=Math.floor(totalSeconds/3600);
hr2=hours%10;
hr1=Math.floor(hours/10)%10;
minutes=Math.floor((totalSeconds-hours*3600)/60);
seconds=totalSeconds-hours*3600-minutes*60;
sec2=seconds%10;
sec1=Math.floor((seconds/10))%10;
min2=minutes%10;
min1=Math.floor((minutes/10))%10;
$('#h1').html(hr1);
$('#h2').html(hr2);
$('#m2').html(min2);
$('#m1').html(min1);
$('#s2').html(sec2);
$('#s1').html(sec1);
}


//UPDATE COUNT DOWN EVERY SECOND
setInterval(function()
{
currentTime--;
if(currentTime>=30)
	{
		countDown(currentTime);

	}
else {
	$('#submitDis').hide();
	//$('#timeRemaining').hide();
	$('#submit').show();
}//DO OTHER STUFF HERE
},1000);

//UPDATE TIME IN DATABASE EVERY THIRTY SECONDS
setInterval(function()
{
if(currentTime>=0)
$.post("components/com_quiz/quizTime.php",{action:"updateTime",userID:userid,quizID:quizid,newTime:currentTime,segmentNum:segment,rand:Math.random()},function(updatedTiming){});	
},updateIntervals);

HERE IS quizTime.php

require_once('config.php');

$con = @mysql_connect($db_host, $db_username, $db_password);
@mysql_select_db($db_database, $con);

if(isset($_POST['action']) && !empty($_POST['action']))
{
$action = $_POST['action'];
if($action == "getTime") getTime();
if($action == "updateTime") updateTime();
}

function createRecord($userid,$quiz,$time,$segment){
mysql_query("INSERT INTO jos_quiz_user_sessions (userid,quiz,time,segment) VALUES(" . $userid . "," . $quiz . "," . $time . "," . $segment . ")");
}

function findValue($userid,$quizid,$segment){
$result = mysql_query("SELECT id FROM jos_quiz_user_sessions WHERE userid = '" . $userid . "' AND quiz = '" . $quizid . "' AND quiz = '" . $segment . "' LIMIT 1;") or die(mysql_error());
$row = mysql_fetch_assoc($result);
if($row['id']) return 1;
else return 0;
}

function getTime(){
$userid			= $_POST['userID'];
$quizid			= $_POST['quizID'];
$defaultTime	= $_POST['defTime'];
$segment		= $_POST['segmentNum'];
if(!findValue($userid,$quizid,$segment)) createRecord($userid,$quizid,$defaultTime,$segment);
$result = mysql_query("SELECT time FROM jos_quiz_user_sessions WHERE userid = '" . $userid . "' AND quiz = '" . $quizid . "' AND segment = '" . $segment . "'");
$i = mysql_fetch_row($result);
$currentTime = $i[0];
echo $currentTime;
}

function updateTime(){
$userid			= $_POST['userID'];
$quizid			= $_POST['quizID'];
$newTime		= $_POST['newTime'];
$segment		= $_POST['segmentNum'];
mysql_query("UPDATE jos_quiz_user_sessions SET time = " . $newTime . " WHERE userid = '" . $userid . "' AND quiz = '" . $quizid . "' AND segment = '" . $segment . "'");
}

mysql_close($con);

Hello Scott,

 

Thanks for the reply, not sure, we are getting reports from our end users on older systems running XP and IE7 / IE8, I would tell them to upgrade to Firefox or chrome but site owner understandably does not like that idea.  Just thought I would post the code up on here and see if anything jumped out at anyone, I'm a PHP guy learning the JavaScript basics.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.