2
Hello, I’m developing an online proof system as the following image:
The questions come straight from the database (MYSQL) and so mount the proof displaying question by question, when the user marks the answer and clicks on "Next" advances to the next question.
My question is this: how will I store in the same variable, be it array or anything, all the answers of the proof?
This is the code of my form:
<?php
require_once('validasessao.php');
$codigo = $_GET['codigo'];
include_once("./classes/conexao.class.php");
include_once("./classes/avaliacoes.class.php");
include_once("./classes/QuestoesDisciplinas.class.php");
$conn = new Conexao();
$questaoDisciplina = new QuestoesDisciplinas($codigoDisciplina, $codigoQuestao);
$avaliacao = new Avaliacoes($codigoAvaliacao, $codigoDisciplina, $dataAvaliacao, $horaInicioAvaliacao, $horaTerminoAvaliacao, $situacaoAvaliacao);
#localizar a avaliação
if($avaliacao->localizarAvaliacao($codigo)){
#pegar os códigos
$codigoAvaliacao = $avaliacao->getCodigoAvaliacao();
$codigoDisciplina = $avaliacao->getCodigoDisciplina();
#buscar as questões
$res = $questaoDisciplina->buscarQuestoesDisciplina($codigoDisciplina);
$array = mysql_fetch_assoc($res);
if($_POST['numeroQuestao'] == 0) {
$numeroQuestao = 1;
} else {
$numeroQuestao = $_POST['numeroQuestao'];
$respostaUsuario = $_POST['respostaUsuario'];
#$respostas[$numeroQuestao-1] = $respostaUsuario;
#print_r($respostas);
}
if($numeroQuestao <= $array['quantidadeQuestoesDisciplina']){
<form name = "prova" id = "prova" action = "?pag=provas-p.php&codigo=<?php echo $codigo ?>" method = "POST">
<table>
<tr>
<td><b>Questao <?php echo $numeroQuestao . "/". $array['quantidadeQuestoesDisciplina'] ?></b></td>
</tr>
<tr>
<td><?php echo $array['descricaoQuestao'] ?></td>
</tr>
<tr>
<td>A) <input type = "radio" name = "respostaUsuario" value = "A"><?php echo $array['descricaoResposta1Questao']?></td>
</tr>
<tr>
<td>B) <input type = "radio" name = "respostaUsuario" value = "B"><?php echo $array['descricaoResposta2Questao']?></td>
</tr>
<tr>
<td>C) <input type = "radio" name = "respostaUsuario" value = "C"><?php echo $array['descricaoResposta3Questao']?></td>
</tr>
<tr>
<td>D) <input type = "radio" name = "respostaUsuario" value = "D"><?php echo $array['descricaoResposta4Questao']?></td>
</tr>
<tr>
<td>E) <input type = "radio" name = "respostaUsuario" value = "E"><?php echo $array['descricaoResposta5Questao']?></td>
</tr>
<tr>
<td><br></td>
</tr>
$numeroQuestao++;
<tr>
<td>
<input type = "hidden" name = "numeroQuestao" id = "numeroQuestao" value = "<?php echo $numeroQuestao ?>">
<input type = "submit" name = "enviar" id = "enviar" value = "Próximo">
</td>
</tr>
</table>
</form>
I just want to save the user’s answer, but I can’t do it.
Can you give more details? How does your form work? Use ajax or reload every question? And you want to store these values on the server or client?
– bfavaretto
when clicking on "next" he reloads the page with the form and save the value of the reply I sent for $_POST[], I’m trying to save in an array with indices equal to the number of questions and even then he only saving the last answer
– Matheus Luz
If you’re doing a post for each time the 'Next' is clicked, then you’re calling the action page again, it won’t keep the values that were there. You can put in an array created in the scope of
session
and in the end uses the array to save the answers to the database. .– Daniel Ribeiro
"You can put it in an array created in the scope of Session and at the end use the array to save the answers to the database" - As well?
– Matheus Luz
Here’s what you do. Put all the code you have on that page on your question, but not as a print screen. Put as text even so we can use to elaborate an answer for you.
– Daniel Ribeiro
Okay, I edited and I put the code
– Matheus Luz
The problem is to store the previous answers, but refresh the page with each question, so how would I keep this data?
– Matheus Luz
Instead of using a simple array, like $answers[], use an array of Session, $_SESSION["answers"][]... and you get $_SESSION["answers"][1] for question 1, $_SESSION["answers"][2] and so on. Just remember to sign in to php’s FIRST line with session_start();
– Marcelo Gomes