PHP: Notice: Undefined index:

1

I have a mini quiz to run where different questions are asked whether it is male or female, I was able to create the questions using radio buttons and their 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 verifies the sex and prints the 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 get the value of the radio buttons.

    
asked by anonymous 02.09.2018 / 05:38

2 answers

1

In order for the button to capture the information filled in the form directly, it must be between the form's tags and must be of type "submit". Putting the button the way it is, it will only redirect to the page that was placed in the href.

Below is an example quiz.php page that will solve your problem:

<!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>
   <form action="resultado.php" method ="post">
<?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"){  
 ?>

    <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>
     <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>
<br>
<?php
  }
?>
<br>
<a href="index.php">
    <button type="button">VOLTAR</button>
</a>
    <button type="submit">RESULTADO</button>
 </form>
</body>
</html>
    
02.09.2018 / 16:31
0
What happens is that the input values of your html form are not arriving in your quiz.php file, so when you try to assign the variable with an index that does not exist in the $_POST array, the Undefined index .

  

$_POST is an associative array of variables passed to the current script via the HTTP POST method when using application / x-www-form-urlencoded or multipart / form-data as the content-type HTTP header value in the request

Before assigning a variable to a value posted via $_GET or $_POST you need to check if this variable exists using the isset() function to check if the POST parameters are being received by your PHP file:

if (isset($_POST["nome"])) {
    $nome = $_POST['nome'];
}

Or you could do it this way, saving a few lines:

$nome = (isset($_POST['nome']) ? $_POST['nome'] : null);

Variable Manipulation Functions - isset

I recommend that you edit your question and add the complete content of your hmtl form and the file quiz.php

I also recommend that you read about filter_var and filter_input to validate and handle the data received from the user.

    
02.09.2018 / 06:07