I can not save form data

0

I am not able to save my form data to the bank.

I have the following files:

functions.php

/**
 *  Cadastro de Notas
 */
function add() {
  if (!empty($_POST['nota'])) {

    $today = 
      date_create('now', new DateTimeZone('America/Sao_Paulo'));
    $nota = $_POST['nota'];
    $aluno = $_POST['nota'];
    $nota['modified'] = $nota['created'] = $today->format("Y-m-d H:i:s");

    save('notas', $nota);
    header('location: index.php');
  }
}

add.php

<?php 
  require_once('functions.php'); 
  add();
  index2();
  ?>

<?php include(HEADER_TEMPLATE); ?>

<h2>Cadastrar Nota</h2>

<form action="add.php" method="post">
  <!-- area de campos do form -->
  <hr />
  <div class="row">
    <div class="form-group col-md-7">
      <label for="name">Aluno</label>


      <select class="form-control">

      <?php if ($alunos) : ?>
      <?php foreach ($alunos as $aluno) : ?>  

      <option value="<?php echo $aluno['AlunoID']; ?>" name="nota['AlunoID']"><?php echo $aluno['AlunoNome']; ?></option>

      <?php endforeach; ?>
      <?php else : ?>

      <option>SEM RESULTADOS</option>

      <?php endif; ?>

      </select>


    </div>
      <div class="form-group col-md-7">
      <label for="name">Materia</label>
      <input type="text" class="form-control" name="nota['MateriaID']">
    </div>
    <div class="form-group col-md-7">
      <label for="name">Nota</label>
      <input type="text" class="form-control" name="nota['Nota']">
    </div>

  </div>

  <div class="row">
     <div class="form-group col-md-2">
      <label for="campo3">Data de Cadastro</label>
      <input type="text" class="form-control" name="nota['created']" disabled>
    </div>
  </div>



  <div id="actions" class="row">
    <div class="col-md-12">
      <button type="submit" class="btn btn-primary">Cadastrar</button>
      <a href="index.php" class="btn btn-default">Cancelar</a>
    </div>
  </div>
</form>

<?php include(FOOTER_TEMPLATE); ?>

What can it be?

Any help will be welcome.

Thank you for your attention.

    
asked by anonymous 08.06.2017 / 04:04

1 answer

1

I believe your form has some errors.

<form action="add.php" method="post">
  <hr />
  <div class="row">
    <div class="form-group col-md-7">
      <label for="name">Aluno</label>

      <select class="form-control">
        <?php if ($alunos) : ?>
          <?php foreach ($alunos as $aluno) : ?>  
            <option value="<?php echo $aluno['AlunoID']; ?>" name="nota['AlunoID']"><?php echo $aluno['AlunoNome']; ?></option>

          <?php endforeach; ?>
        <?php else : ?>
        <option>SEM RESULTADOS</option>
        <?php endif; ?>
      </select>
    </div>

    <div class="form-group col-md-7">
      <label for="name">Materia</label>
      <input type="text" class="form-control" name="nota['MateriaID']">
    </div>

    <div class="form-group col-md-7">
      <label for="name">Nota</label>
      <input type="text" class="form-control" name="nota['Nota']">
    </div>
  </div>

  <div class="row">
    <div class="form-group col-md-2">
      <label for="campo3">Data de Cadastro</label>
      <input type="text" class="form-control" name="nota['created']" disabled>
    </div>
  </div>

  <div id="actions" class="row">
    <div class="col-md-12">
      <button type="submit" class="btn btn-primary">Cadastrar</button>
      <a href="index.php" class="btn btn-default">Cancelar</a>
    </div>
  </div>
</form>
  • Your select element does not have the name attribute set. Instead, you set the attribute to option . Remove from option and set it to select :

    <select class="form-control" name="nota['AlunoID']">
       ...
    </select>
    
  • The nota['created'] field is as disabled , so its value will not be sent in the form submission. This is not an error, since in the function you redefine this value, but it does not make much sense to have an empty field turned off by default in the form.

  • Within the function add , you can comment the line header and put var_dump($nota) to check if the form data is arriving as expected.

        
    08.06.2017 / 04:20