"Undefined index" message when accessing $_POST index

Asked

Viewed 671 times

0

<div class="modal fade" id="adcmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="exampleModalLabel">Adicione a nota dos seus alunos</h2>
      </div>
      <div class="modal-body">
        <form method="post" action= "index.php">
          <div class="form-group">
            <label for="nome" class="control-label">Nome:</label>
            <input type="text" class="form-control" name="nome">
          </div>
          <div class="form-group">
            <label for="nota1" class="control-label">Nota1:</label>
            <input type="text" class="form-control" id="nota1">
          </div>
          <div class="form-group">
            <label for="nota2" class="control-label">Nota2:</label>
            <input type="text" class="form-control" id="nota2">
          </div>
          <div class="form-group">
            <label for="nota3" class="control-label">Nota3:</label>
            <input type="text" class="form-control" id="nota3">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <form><button type="button" class="btn btn-lg btn-danger" data-dismiss="modal">Fechar</button>
        <input type="submit" name="addbtn" value="Adicionar" class="btn btn-lg btn-sample"></form>

      </div>
      </div>

  </div>
</div>
     <?php
    include("config.php");
    $con = mysqli_connect("localhost","root","","alunos");
    $nome = $_POST['nome'];

    if (isset($_GET['addbtn'])) {

    $query = "INSERT INTO `aluno` (`id`, `nome`, `nota`, `nota2`, `nota3`) VALUES (NULL, $nome, '10', '20', '30')";

    mysqli_query($con, $query);
    }

    ?>

What’s wrong that’s not detecting the part $name = $_POST['name'];?

Notice: Undefined index: nome in C: xampp htdocs index.php on line 112

  • 2

    The POST only exists at the time you submit the form, so you also have to check if there is the $_POST['name']

  • if (isset($_GET['addbtn']) && isset($_POST['name'])) {

  • You place the Submit button outside the first <form></form> block which is the one containing the fields. Try to put inside it and you can remove the second <form></form block>.

1 answer

2


The first time you access the page, you are making an HTTP request using the GET method, so no information will be defined in the global variable $_POST. This way, when you try to get the value of $_POST["nome"], it does not exist, generating the error. You can only access this value if PHP is handling an HTTP request with the POST method. To do this check, just do:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    ...
}

In your case, it would look something like:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    include("config.php");
    $con = mysqli_connect("localhost","root","","alunos");
    $nome = $_POST['nome'];

    if (isset($_POST['addbtn'])) {
        $query = "INSERT INTO `aluno` (`id`, `nome`, `nota`, `nota2`, `nota3`) VALUES (NULL, $nome, '10', '20', '30')";
        mysqli_query($con, $query);
    }
}

?>

Notice that I changed also of $_GET["addbtn"] for $_POST["addbtn"] because it would be the most logical for this case. In fact, in its HTML code, the button addbtn is in a different form from the fields. I don’t see it making much sense. I believe you should leave it in the same form as the text fields.

  • Oops, thank you very much, just one detail: printing the variable $name in the insert I should use this way '".$name."' , right? Or is there another?

  • @Skullfire Using double quotes to string PHP can parse variables inside it, not needing to concatenate. So something like "insert into ... values (null, '$nome')" would already work too.

  • As incredible as it seems it didn’t work that way

  • @Skullfire made some mistake?

  • No, just not in the table. Using '".$name."' good-bye.

  • 1

    It would have like the POST to be without data. For example curl -X POST https://site.com (or with other data, without the nome). That would keep firing the notice in the $nome = $_POST['nome'];, why it would still not exist, despite the method being POST. I believe that it would be ideal a isset($_POST['nome']).

Show 1 more comment

Browser other questions tagged

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