Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,491 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,192 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



automatically submition of quiz after certain period of time

 
Reply to this topicStart new topic

automatically submition of quiz after certain period of time

kaps
post 17 Jun, 2008 - 03:03 AM
Post #1


New D.I.C Head

*
Joined: 31 May, 2008
Posts: 22

hello frnds,
i want to run a quiz competion on my website in which after logging any user play that quiz, like i will give 50 question to solve in just 3 min time, all will be objective question, now after that 3 min, quiz will automatically end and all users marked answer entered to the database, means he has not press any submit button, but after specific time all answer automatically enter in the database, and he come out from it..

now im using this timer,
CODE



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$dateFormat = "d F Y -- g:i a";
$targetDate = time() + (1*60);//Change the 25 to however many minutes you want to countdown
$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quiz System</title>

<script type="text/javascript">
  var days = <?php echo $remainingDay; ?>  
  var hours = <?php echo $remainingHour; ?>  
  var minutes = <?php echo $remainingMinutes; ?>  
  var seconds = <?php echo $remainingSeconds; ?>
function setCountDown ()
{
  seconds--;
  if (seconds < 0){
      minutes--;
      seconds = 59
  }
  if (minutes < 0){
      hours--;
      minutes = 59
  }
  if (hours < 0){
      days--;
      hours = 23
  }
  document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
  SD=window.setTimeout( "setCountDown()", 1000 );
  if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
           //window.alert("Time is up. Press OK to continue."); // change timeout message as required
          window.location = "http://www.lucknowmail.com" // Add your redirect url
    }

}

</script>
</head>
<body onload="setCountDown();">

Start Time: <?php echo $actualDateDisplay; ?><br />
End Time:<?php echo $targetDateDisplay; ?><br />
<div id="remain"><?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></div>

  
</body>
</html>


</body>
</html>


pl help me , and tell me where to bring changes or which new thing is to be added
User is offlineProfile CardPM

Go to the top of the page


1lacca
post 17 Jun, 2008 - 03:45 AM
Post #2


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,721



Thanked 9 times
My Contributions


Moved to javascript.
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 17 Jun, 2008 - 06:25 PM
Post #3


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,260



Thanked 38 times

Dream Kudos: 775
My Contributions


Instead of telling it to redirect you wll be having a form in the html to get the quiz and everything together, if you don't you should.

The form may look like so:

CODE
<script language ="JavaScript">
function SubmitForm()
{
document.form1.action = "http://www.google.com";
document.form1.submit()
}
function ResetForm()
{
document.form1.reset()
}
</script>
<form name="form1" method="post" action="">
<a href="java script: SubmitForm()">Test Link</a>
</form>


All you have to change for the final thing is where the action is pointing. Add all of your questions into the form and allow for all the answers to be chosen in the form and change where the action is pointing to in the end. (you can place the form action in the HTML if you wish I just found it easier to use in the javascript).
User is offlineProfile CardPM

Go to the top of the page

kaps
post 17 Jun, 2008 - 11:40 PM
Post #4


New D.I.C Head

*
Joined: 31 May, 2008
Posts: 22

thanx betaware,

as im very new in programming, but ur suggestion must help me, im trying to run the script, and will let u later what the results come
User is offlineProfile CardPM

Go to the top of the page

kaps
post 18 Jun, 2008 - 12:00 AM
Post #5


New D.I.C Head

*
Joined: 31 May, 2008
Posts: 22

hi betaware,

i use your code and ur suggestion but it still not working..

i paste your code and in action i redirect it to my insert.php page, where database and table are there...

CODE
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("radio", $con);
  $sql="INSERT INTO radio (q1, q2)
VALUES
('$_POST[question1]','$_POST[question2]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "your quiz is submitted";mysql_close($con)
?>


now the result is not submitting directly to the database, also still im not using timer, as i want that after certain period of time like after 2 min all the result automatically submitted to the databse and the user come out the quiz page..

pl help me..
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 18 Jun, 2008 - 12:11 PM
Post #6


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,260



Thanked 38 times

Dream Kudos: 775
My Contributions


For you php (just good practice that may be causing some issues) try making sure that you call to arrays with some sort of quotes around them:

QUOTE
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("radio", $con);
$sql="INSERT INTO radio (q1, q2)
VALUES
('" . $_POST['question1'] . "','" . $_POST['question2'] . "')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "your quiz is submitted";
mysql_close($con);
?>


Also look into the website security tutorial if you have the time, it could save you a lot of hard work if someone was to inject malicious content into your queries.
http://www.dreamincode.net/forums/showtopic54760.htm (has a little on it, but is not all about injection protection)

Also, in your form you probably have radio buttons, what you need to do is name each radio button for each question the same thing (liek question1), that is the name you are calling in the $_POST['question1'].

So your form should look something like so:

CODE

<form name="form1" action="" method="post">
Q1: What is 1+1?
<input type="radio" name="question1" value="2">2
<input type="radio" name="question1" value="11">11
<input type="radio" name="question1" value="1">1
<input type="radio" name="question1" value="0">0
and so forth
</form>


Than, to gett the auto submit working with your countdown code you just do something liek so:

QUOTE
<script type="text/javascript">
var days = <?php echo $remainingDay; ?>
var hours = <?php echo $remainingHour; ?>
var minutes = <?php echo $remainingMinutes; ?>
var seconds = <?php echo $remainingSeconds; ?>
function setCountDown ()
{
seconds--;
if (seconds < 0){
minutes--;
seconds = 59
}
if (minutes < 0){
hours--;
minutes = 59
}
if (hours < 0){
days--;
hours = 23
}
document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
SD=window.setTimeout( "setCountDown()", 1000 );
if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
//window.alert("Time is up. Press OK to continue."); // change timeout message as required
//window.location = "http://www.lucknowmail.com" // Add your redirect url
SubmitForm();

}

}

function SubmitForm() {
document.form1.action = "insert.php";
document.form1.submit()
}
function ResetForm() {
document.form1.reset()
}

</script>


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

kaps
post 19 Jun, 2008 - 02:30 AM
Post #7


New D.I.C Head

*
Joined: 31 May, 2008
Posts: 22

hi betawar,

first thanx for ur support and ur code but i afraid to say that this is still not running, in fact on my main page, even timer is not showing,nothing is happening

pl help

thanx in advance
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 02:10PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month