Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  133] [ 5]  / answers: 1 / hits: 16562  / 11 Years ago, sun, august 4, 2013, 12:00:00

I wrote the following code. With this code pushing Submit button submits the form manually. I have also a timer which I want to auto submit the form after 10 seconds. But it does not work. It counts until 0 and then it does not do anything. Can you please tell me what I am missing or how to change my timer (if there, is the problem)? But I want the user to watch the timer as my example



<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>

<head>
<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1 />
<script type=text/javascript>
function countDown(secs,elem)
{
var element = document.getElementById(elem);
element.innerHTML = <h2>You have <b>+secs+</b> seconds to answer the questions</h2>;
if(secs < 1){
clearTimeout(timer);
document.getElementById('myquiz').submit();
}
secs--;
var timer = setTimeout ('countDown('+secs+','+elem+')',1500);
}
</script>
<div id=status></div>
<script type=text/javascript>countDown(5,status);</script>
<title>Questionnaire</title>
<style type=text/css>
span {color: #FF00CC}
</style>
</head>
<body>
<h1>Please complete the following Survey</h1>
<form name=quiz id =myquiz method=post action=includes/process.php>
First Name: <input type=text name=firstname id=fname/>
<p></p>
Last Name: <input type=text name=lastname id=lname/>
<p></p>
<input type=submit name=submit value=Go></input>
<input type=reset value=clear all></input>
</form>
</body>
</html>

More From » php

 Answers
124

Don't do this:



var timer = setTimeout ('countDown('+secs+','+elem+')',1500);


in countDown. Every 1500 you're calling countDown again.



Put this at the bottom of the page (before closing body tag)



<script type=text/javascript>
secs = 10;
timer = setInterval(function () {
var element = document.getElementById(status);
element.innerHTML = <h2>You have <b>+secs+</b> seconds to answer the questions</h2>;
if(secs < 1){
clearInterval(timer);
document.getElementById('myquiz').submit();
}
secs--;
}, 1000)




Btw: where is validate() declared ?



Didn't test it, but it should do the trick.


[#76531] Friday, August 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;