I can’t save form data

Asked

Viewed 35 times

0

personal I am unable to save my form data in 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); ?>

inserir a descrição da imagem aqui

what can be?

any help will be welcome.

since thanks for your attention.

1 answer

1


I believe your form is with 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>
  1. His element select does not have the attribute name defined. Instead, you set the attribute to option. Remove from option and define it in select:

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

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

  • Thanks Anderson! had set the name attribute in the wrong place was in the same select. Thanks even Brother!

Browser other questions tagged

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