Undefined index php and html

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 is not detecting the part $ name = $ _POST ['name']; ?

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

    
asked by anonymous 21.05.2017 / 02:25

1 answer

1

The first time you access the page, you are making an HTTP request using the GET method, so no information is set on 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 also changed from $_GET["addbtn"] to $_POST["addbtn"] because it would be more logical for this case. In fact, in its HTML code, the addbtn button is in a form other than the fields. I do not see that making much sense. I believe you should leave it in the same form as the text fields.

    
21.05.2017 / 02:34