0
I have a mini quiz to run where are different questions if it is male or female, I managed to create the questions using radio Uttons and its value being "right" and "wrong" and in php I used something around "if(variable == "right"){ $result += +1}"
Super Quiz:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Super Quiz</title>
<style>
h1{
text-align: center;
font-family: Comic Sans,Helvetica, fixedsys;
}
p{
text-align: center;
}
#teste{
padding-top: 5%;
padding-left: 5%;
padding-right: 5%;
padding-bottom: 5%;
}
</style>
</head>
<body>
<form action = "quiz.php" method = "post">
<div id="teste">
<fieldset>
<h1> Super Quiz </h1>
<br>
<p>Nome: </p>
<p><input type = "text" name = "nome" value = "" size="40" maxlength="40"> </p>
<br>
<p>Informe seu sexo:</p>
<br>
<p><input type ="radio" name = "sexo" value = "m" checked> MASCULINO </p>
<br>
<p><input type ="radio" name = "sexo" value = "f"> FEMININO </p>
<br><br>
<p><input type = "submit" value="Enviar"></p>
</fieldset>
</div>
</form>
</body>
</html>
PHP page that checks sex and prints questions:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Super Perguntas </title>
<style>
h1{
text-align: center;
font-family:Comic-Sans, Arial, Helvetica, sans-serif;
}
p{
text-align: center;
}
</style>
</head>
<body>
<?php
$nome = $_POST['nome'];
$sexoEscolhido = $_POST['sexo'];
if($sexoEscolhido == "m")
echo "<h1> Seja bem vindo, $nome </h1><br>";
else
echo "<h1> Seja bem vinda, $nome </h1><br>";
echo "<br>" . $nome . ", responda a estas perguntas: <br><br>";
if($sexoEscolhido == "m"){
?>
<form action="resultado.php" method ="post">
<div id="teste">
<p>Qual o time brasileiro tem mais mundiais?</p>
<p><input type="radio"; name="um" value= "errada" checked>1- Santos </p><br>
<p><input type="radio"; name="um" value = "errada">2- Corinthians </p><br>
<p><input type="radio"; name="um" value = "certa">3- São Paulo </p><br>
<p><input type="radio"; name="um" value = "errada">4- Palmeiras </p><br>
<br>
<br>
<br>
<p>Qual o nome do maior continente do mundo?</p>
<p><input type="radio"; name="dois" value="certa" checked>1- Ásia </p><br>
<p><input type="radio"; name="dois" value="errada">2- Oceania </p><br>
<p><input type="radio"; name="dois" value="errada">3- America do Sul </p><br>
<p><input type="radio"; name="dois" value="errada">4- Antartida </p><br>
<br>
<br>
<br>
<p>Qual o nome da espécie do Optimus Prime?</p>
<p><input type="radio"; name="tres" value="errada" checked>1-Transformer </p><br>
<p><input type="radio"; name="tres" value="errada">2-Autobots </p><br>
<p><input type="radio"; name="tres" value="errada">3-Decepticons </p><br>
<p><input type="radio"; name="tres" value="certa">4- Cybertroniano </p><br>
<br>
</div>
</form>
<br>
<?php
}
else{
?>
<form action = "resultado.php" method ="post">
<div id="teste">
<p>Qual o nome da neta do Goku?</p>
<p><input type="radio" name="um" value="certa" checked>1-Pan </p><br>
<p><input type="radio" name="um" value="errada">2-Videl </p><br>
<p><input type="radio" name="um" value="errada">3-Bulma </p><br>
<p><input type="radio" name="um" value="errada">4-Sakura </p><br>
<br>
<br>
<br>
<p>Qual o jogo mais jogado do mundo?</p>
<p><input type="radio" name="dois" value="errada" checked>1-Counter Strike: Global Ofensive</p><br>
<p><input type="radio" name="dois" value="certa">2-League of Legends</p><br>
<p><input type="radio" name="dois" value="errada">3-Fortnite</p><br>
<p><input type="radio" name="dois" value="errada">4-Transformice</p><br>
<br>
<br>
<br>
<p>Qual o último campeão do campeonato brasileiro, do ano de 2017?</p>
<p><input type="radio" name="tres" value="errada" checked>1-Internacional </p><br>
<p><input type="radio" name="tres" value="errada">2-Santos </p><br>
<p><input type="radio" name="tres" value="errada">3-Palmeiras </p><br>
<p><input type="radio" name="tres" value="certa">4-Corinthians </p><br>
<br>
</div>
</form>
<br>
<?php
}
?>
<br>
<a href="index.php">
<button>VOLTAR</button>
</a>
<a href="resultado.php">
<button>RESULTADO</button>
</a>
</body>
</html>
Page that should contain the result:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Super Quiz</title>
<style>
h1{
text-align: center;
font-family: Comic Sans,Helvetica, fixedsys;
}
p{
text-align: center;
}
#teste{
padding-top: 5%;
padding-left: 5%;
padding-right: 5%;
padding-bottom: 5%;
}
</style>
</head>
<body>
<?php
//inicialização
$um = "";
$dois = "";
$tres = "";
$resultado = 0;
//pegar variável do post
$perguntaUm = $_POST["um"];
$perguntaDois = $_POST["dois"];
$perguntaTres = $_POST["tres"];
//estrutura de verificação
if($perguntaUm == "certa"){
$resultado += +1;
}
if($perguntaDois == "certa"){
$resultado += +1;
}
if($perguntaTres == "certa"){
$resultado += +1;
}
echo $resultado;
?>
<?php
echo
"<form>
<div id=\"teste\">
<fieldset>
<h1> Super Quiz </h1>
<br>
<p>Nome: </p>
<br>
<p>Neste quiz você acertou um total de: $resultado </p>
</fieldset>
</div>
</form>";
?>
<a href="index.php">
<button>VOLTAR</button>
</a>
</body>
</html>
What happens is the error "Notice: Undefined index:" in the three variables that should take the value of radio Buttons.
Really, putting the boot inside the form was the key, I managed to complete with your help, and learned much more by researching alternative ways to solve, here I leave my most sincere gratitude of a student!
– Vitor Gomes
@Vitorgomes if André’s answer is correct. Mark it as right. Only you have this power.
– Andrei Coelho