PHP: Notice: Undefined index:

Asked

Viewed 1,431 times

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.

2 answers

1


In order for the button to execute capturing the information filled in the form directly, it must be between the form tags and be of type "Submit". By putting the button the way it is, it will only redirect to the page that was placed on href.

Below is an example of the quiz.php page, which 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>
  • 1

    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!

  • @Vitorgomes if André’s answer is correct. Mark it as right. Only you have this power.

0

What happens is that the input values of your html form are not getting into your quiz.php file, so when you try to assign the variable with an index that does not exist in the array $_POST error occurs 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 value of the HTTP Content-Type header in the request.

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

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

or could still do so, saving some lines:

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

Functions for Variable Manipulation - isset

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.