hallo there,

i ve coded a script that the user can write a test. there is a form that he completes the answears and a button that posts those answears to another file which connects to a database checks if he is right and then shows the results.

Now i want to use a timer in order to autosubmit the form after some time (ex 15 minutes). I know that i need a timer countdown( the user must see all the time the time left) and i think that i should use javascript. how can i sudmit the form after the time ends in order the user to see the results that he made after this time passed??

......
.......
.....
.....

d1=new Date();
		d1.setMinutes ( d1.getMinutes() + 15;
		
		var etime = new Date(d1);
		var ehour = etime.getHours();
		var emin = etime.getMinutes();
		var esec = etime.getSeconds();
		var ftime = "";
		
		if(ehour == 0) ehour = 12;
		ftime = (ehour > 12 ? ehour - 12 : ehour) + ":" +
		(emin < 10 ? "0" : "") + emin + ":" +
		(esec < 10 ? "0" : "") + esec + " " +
		(ehour > 12 ? "PM" : "AM");

		document.getElementById("etime").value = ftime;
......
......
......

setInterval("settime()", 1000);
 	function settime () 
	{
		var curtime = new Date();
		var curhour = curtime.getHours();
		var curmin = curtime.getMinutes();
		var cursec = curtime.getSeconds();
		var time = "";
		
		if(curhour == 0) curhour = 12;
		time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
		(curmin < 10 ? "0" : "") + curmin + ":" +
		(cursec < 10 ? "0" : "") + cursec + " " +
		(curhour > 12 ? "PM" : "AM");
		
		document.getElementById("stime").value = time;
		
		etime=document.getElementById("etime").value;
		
		if(etime!="")
		{
			if(time>=etime)
			{
				document.getElementById("submit").click();
			}	
		}
	}
.....
.....
<div class="textfield_container_right">
            <div class="text1" style="width:90px;"><strong>Time	 :</strong></div>
            <div class="text1"><input type="text" name="stime" id="stime" style="border:none; background-color:transparent; color:#FFFFFF; margin-top:5px;" readonly="readonly"/></div>
            </div>
            <div class="textfield_container_right">
            <div class="text1" style="width:90px;"><strong>End Time :</strong></div>
            <div class="text1"><input type="text" name="etime" id="etime" readonly="readonly" style="border:none; background-color:transparent; color:#FFFFFF; margin-top:5px;"/></div>
.....
.....
.....

<input type="submit" name="submit" id="submit" />

Close bracketr is missing on line 7 of javascript

Thanks for the answear
let me test this and ill post the result

ok, let me write some questions about this code. first i wanna say that i am new at php and javascript so my experience is not helping me at all

a) i copy and pasted the javascript code you wrote to a file: time.js and i called it inside the initial php file.

<script	type="text/javascript"
	src="time.js"
></script>

inside the code there are some dots. There r their because u expect me to write code that is obvious to u? i just erase those dots when i created the file i mentioned.

b)in the php code, is it possible to post the css classes you use? (textfield_container_right, text1, stime, etime etc...)

C) i also can get how the form posts the answears. you just

<input type="submit" value="sudmit" id="submit">

i can not see how the javascript connects with the php code.

(i told you i am an amateur :-) )

is it possible to pass a parametre from the initial php file to javascript file in order to change the amount of passing time? It will not be always 15'......

thanks anyway.......

No need of that css for text box its just as

Time :<input type="text" name="stime" id="stime" readonly="readonly"/><br>
End Time :<input type="text" name="etime" id="etime" readonly="readonly"/>

and call settime() function in body onload like

<body onload="settime()">

thats enough

something is not right with the etime variable because etime input is empty. this way nothing happens after the time ends...
the javascript you gave me:

d1=new Date();
d1.setMinutes ( d1.getMinutes() + 15);
 
var etime = new Date(d1);
var ehour = etime.getHours();
var emin = etime.getMinutes();
var esec = etime.getSeconds();
var ftime = "";
 
if(ehour == 0) ehour = 12;
ftime = (ehour > 12 ? ehour - 12 : ehour) + ":" +
(emin < 10 ? "0" : "") + emin + ":" +
(esec < 10 ? "0" : "") + esec + " " +
(ehour > 12 ? "PM" : "AM");
 
document.getElementById("etime").value = ftime;

 
setInterval("settime()", 1000);
function settime ()
{
var curtime = new Date();
var curhour = curtime.getHours();
var curmin = curtime.getMinutes();
var cursec = curtime.getSeconds();
var time = "";
 
if(curhour == 0) curhour = 12;
time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
(curmin < 10 ? "0" : "") + curmin + ":" +
(cursec < 10 ? "0" : "") + cursec + " " +
(curhour > 12 ? "PM" : "AM");
 
document.getElementById("stime").value = time;
 
etime=document.getElementById("etime").value;
 
if(etime!="")
{
if(time>=etime)
{
document.getElementById("submit").click();
}
}
}

write the js code inside the php page.
dont include from js file

Use the script at the end of the page as follows

<!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=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<form method="post" name="frm_answer">
    Time :<input type="text" name="stime" id="stime" readonly="readonly"/><br>
    End Time :<input type="text" name="etime" id="etime" readonly="readonly"/>
</form>
</body>
</html>
<script type="text/javascript">
	d1=new Date();
d1.setMinutes ( d1.getMinutes() + 15);
 
var etime = new Date(d1);
var ehour = etime.getHours();
var emin = etime.getMinutes();
var esec = etime.getSeconds();
var ftime = "";
 
if(ehour == 0) ehour = 12;
ftime = (ehour > 12 ? ehour - 12 : ehour) + ":" +
(emin < 10 ? "0" : "") + emin + ":" +
(esec < 10 ? "0" : "") + esec + " " +
(ehour > 12 ? "PM" : "AM");
 
document.getElementById("etime").value = ftime;
 
 
setInterval("settime()", 1000);
function settime ()
{
var curtime = new Date();
var curhour = curtime.getHours();
var curmin = curtime.getMinutes();
var cursec = curtime.getSeconds();
var time = "";
 
if(curhour == 0) curhour = 12;
time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
(curmin < 10 ? "0" : "") + curmin + ":" +
(cursec < 10 ? "0" : "") + cursec + " " +
(curhour > 12 ? "PM" : "AM");
 
document.getElementById("stime").value = time;
 
etime=document.getElementById("etime").value;
 
if(etime!="")
{
if(time>=etime)
{
//alert("kartik");
document.getElementById("submit").click();
}
}
}
</script>

:-)
IT WORKS MAN. IT IS PERFECT!! EXACTLY WHAT I NEEDED!! THANK YOU VERY MUCH........

ONE MORE THINK.

I WANT TO PASS THE PERIOD TIME (15 MINUTES) INTO JAVASCRIPT USING A VARIABLE.
IS IT POSSIBLE???

<script type="text/javascript">
d1=new Date();
d1.setMinutes ( d1.getMinutes() + $variable);

there r several tests and every test needs different time. At the begin of the initial php file i will get from a database the time into a variable. I want to call that javascript using that variable.....

do u think that i should start a new thread about passing variables into javascript from php? i tried several thinks i found google searhing but nothing seems to work..

Yes Possible... Its as follows

<!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=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<form method="post" name="frm_answer">
    Time :<input type="text" name="stime" id="stime" readonly="readonly"/><br>
    End Time :<input type="text" name="etime" id="etime" readonly="readonly"/>
</form>
</body>
</html>
<?php
        //Your db connection code
	$qry_interview="select * from tbl_name where id=1";//Pass the id whatever u need
	$rs_interview=mysql_query($qry_interview);
?>
<script type="text/javascript">
	d1=new Date();
d1.setMinutes ( d1.getMinutes() + <?php echo mysql_result($rs_interview,0,"duration_field"); ?>);// duration fiels is the field name of interview durtation in ur db
 
var etime = new Date(d1);
var ehour = etime.getHours();
var emin = etime.getMinutes();
var esec = etime.getSeconds();
var ftime = "";
 
if(ehour == 0) ehour = 12;
ftime = (ehour > 12 ? ehour - 12 : ehour) + ":" +
(emin < 10 ? "0" : "") + emin + ":" +
(esec < 10 ? "0" : "") + esec + " " +
(ehour > 12 ? "PM" : "AM");
 
document.getElementById("etime").value = ftime;
 
 
setInterval("settime()", 1000);
function settime ()
{
var curtime = new Date();
var curhour = curtime.getHours();
var curmin = curtime.getMinutes();
var cursec = curtime.getSeconds();
var time = "";
 
if(curhour == 0) curhour = 12;
time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
(curmin < 10 ? "0" : "") + curmin + ":" +
(cursec < 10 ? "0" : "") + cursec + " " +
(curhour > 12 ? "PM" : "AM");
 
document.getElementById("stime").value = time;
 
etime=document.getElementById("etime").value;
 
if(etime!="")
{
if(time>=etime)
{
//alert("kartik");
document.getElementById("submit").click();
}
}
}
</script>

Perfect!!! it works fine. Thank u very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.