1
I am making a program in PHP, Javascript and Mysql about questions and answers.
Well, in this code I need to count the time it takes the user to answer the question and store it in a variable to use the value in another code.
Also, is there any way to make the timer start by itself as soon as the page is accessed?
<html>
<head>
<title>Responder</title>
</head>
<body>
<?php
echo"<center>";
echo"<h1>PERGUNTA</h2>";
$Servidor = "localhost";
$BancoDeDados = "DESAFIO";
$Usuario = "root";
$Senha = "123";
$db = mysql_connect ($Servidor,$Usuario,$Senha);
$basedados = mysql_select_db($BancoDeDados);
$consulta ='Select QUESTAO, ALTERNATIVACORRETA, ALTERNATIVA_A, ALTERNATIVA_B, ALTERNATIVA_C, ALTERNATIVA_D, ALTERNATIVA_E from PERGUNTA where pergunta.ID = '.$_GET["ID"].' Order by ID;';
$resultado = mysql_query($consulta) or print (mysql_error());
while ($linha = mysql_fetch_array($resultado))
{
$Questao=$linha["QUESTAO"];
$ALTERNATIVACORRETA=$linha["ALTERNATIVACORRETA"];
$ALTERNATIVA_A=$linha["ALTERNATIVA_A"];
$ALTERNATIVA_B=$linha["ALTERNATIVA_B"];
$ALTERNATIVA_C=$linha["ALTERNATIVA_C"];
$ALTERNATIVA_D=$linha["ALTERNATIVA_D"];
$ALTERNATIVA_E=$linha["ALTERNATIVA_E"];
}
$consulta2 ='Select ID from ALUNO;';
$resultado2 = mysql_query($consulta2) or print (mysql_error());
while ($linha2 = mysql_fetch_array($resultado2))
{
$ALUNO=$linha2["ID"];
}
echo
'
<Form action="responder.php?ID='.$_GET["ID"].'&" method="POST">
<table border=1 align=center>
<font size=25 color=red>'.$Questao.'</font> <p>
<tr>
<td>
A) <input type="radio" name="resposta" class="txt" maxlength="15" value=A>
'.$ALTERNATIVA_A.'
</td>
</tr>
<tr>
<td>
B) <input type="radio" name="resposta" class="txt" maxlength="15" value=B>
'.$ALTERNATIVA_B.'
</td>
</tr>
<tr>
<td>
C) <input type="radio" name="resposta" class="txt" maxlength="15" value=C>
'.$ALTERNATIVA_C.'
</td>
</tr>
<tr>
<td>
D) <input type="radio" name="resposta" class="txt" maxlength="15" value=D>
'.$ALTERNATIVA_D.'
</td>
</tr>
<tr>
<td>
E) <input type="radio" name="resposta" class="txt" maxlength="15" value=E>
'.$ALTERNATIVA_E.'
</td>
</tr>
</form>
</table>
<p>
<input type="submit" value="Responder">
<p>
<a href="INDEX.php"> <font color=red> Principal </font> <a> •
<a href="login_aluno.php"> <font color=red> Aluno </font> <a>
';
?>
<script type="text/javascript" language="JavaScript">
var segundo = 0+"0";
var minuto = 0+"0";
var hora = 0+"0";
function tempo(){
if (segundo < 59){
segundo++
if(segundo < 10){segundo = "0"+segundo}
}else
if(segundo == 59 && minuto < 59){
segundo = 0+"0";
minuto++;
if(minuto < 10){minuto = "0"+minuto}
}
if(minuto == 59 && segundo == 59 && hora < 23){
segundo = 0+"0";
minuto = 0+"0";
hora++;
if(hora < 10){hora = "0"+hora}
}else
if(minuto == 59 && segundo == 59 && hora == 23){
segundo = 0+"0";
minuto = 0+"0";
hora = 0+"0";
}
form.cronometro.value = hora +":"+ minuto +":"+ segundo
}
</script>
<form name="form">
<input type="text" name="cronometro" value="00:00:00" readonly="readonly" />
<br />
<button type="button" onclick="setInterval('tempo()',983);return false;">Iniciar Cronômetro</button>
</form>
</body>
</html>
Rizzo, your code is really messed up, can you tell what you’ve been trying to do? A chronometer ?
– Gabriel Rodrigues
You want the result of the string
hora +":"+ minuto +":"+ segundo
in a variable, or the three variables with their isolated values?– Samir Braga
I edited the question explaining better
– RIZZO
Not related to your problem, but not least: Your system has only 1 student?
– Jorge Campos
No, they’re more than one
– RIZZO
So you have a problem, this consultation:
$consulta2 ='Select ID from ALUNO;';
is bringing all students, and the while you do selects only the latter. Somewhere you should filter that query. Another thing, you shouldn’t use the functionsmysql_*
they have been deprecated, use the functionsmysqli_*
or thePDO
– Jorge Campos