Passing correct parameters on Form submit

1

Hello,

I'm trying to pass some parameters to the controller, but I'm not getting it.

The following code has been changed to load the contents of the clicked tab and display it in an input and texarea.

And it's working and loading correctly, as per the link .

Now I need you to change anything and make the submit, the change be saved in the bank.

But when I submit the form, regardless of the ID I'm changing, I'm getting the following return:

UPDATE 'nota' SET 'titulo' = 'NOTA 5', 'nota' = 'TEXTO NOTA 5', 'usuario_nome' = 'Wagner Fillio', 'usuario_id' = '1', 'dt_alteracao' = 1481891781 WHERE 'id' = '5'

This is because in memento I have 5 notes recorded in bd.

See below my view code

<?php

    $classActive = "";
    $divMenu = "";
    $divPanel = "";

    $this->db->order_by('id', 'asc');
    $this->db->where('usuario_id', $this->session->userdata('id'));
    $this->db->where('usuario_nome', $this->session->userdata('usuario_nome'));
    $nota = $this->db->get('nota')->result_array();
    $contador = 0;
    foreach ($nota as $row) {
        $classActive .= ($contador == 0) ? "active" : "inactive";
        //$divMenu = "<li class=\"" . $classActive . "\"><a href=\"#" . $row['id'] . "\" data-toggle=\"tab\"><i class=\"\"></i>" . $row['titulo'] . "</a></li>";
        $divPanel .= "
        <div class=\"tab-pane " . $classActive . "\" id=\"" . $row['id'] . "\">         
            <div id=\"sample\" class=\"ruledpaper\">
                <div class=\"form-group\" style=\"margin: 0px;\">
                    <div class=\"col-md-12\" style=\"padding:0px; background-color: #FFFCEE; font-size: 5px;\">
                        <input type=\"text\" class=\"form-control\" name=\"id\" value=\"" . $row['id'] . "\">
                        <input type=\"text\" class=\"form-control\" rows=\"14\" style=\"padding: 5px; border:0px; background-color: #fff6cc; font-size: 18px;\" name=\"titulo\" placeholder=\"Título\" value=\"" . $row['titulo'] . "\">
                    </div>
                </div>
                <hr style=\"margin: 0px;\" />
                <div class=\"form-group\">
                    <div class=\"col-md-12\" style=\"padding:0px;\">
                        <textarea maxlength=\"60\" class=\"ruledpaper form-control\" rows=\"\" cols=\"\" style=\"padding: 5px; border:0px; min-height: 350px;\" name=\"nota\" placeholder=\"Digite o texto...\">" . $row['nota'] . "</textarea>
                    </div>
                </div>
            </div>
        </div>";
        $contador++;
    }
?>
<form action="<?= base_url()?>admin/notas/salvar" id="" method="post" class="form-horizontal" >
<div class="row">
    <div class="col-sm-8">
        <div class="tab-content" style="width: 70%;">
            <?php echo $divPanel; ?>
            <div class="form-group">
                <div class="box-header" style="padding-left: 17px">             
                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-primary btn-flat', 'content' => 'Salvar')); ?>
                    <?php echo form_button(array('type' => 'reset', 'class' => 'btn btn-warning btn-flat', 'content' => 'Limpar')); ?>
                    <?php echo anchor('admin/dashboard', 'Limpar', array('class' => 'btn btn-default btn-flat')); ?>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-4">
        <ul class="nav tabs-vertical">
        <?php foreach ($nota as $row){?>            
            <li class="">           
                <a href="#<?php echo $row['id'];?>" data-toggle="tab"> <i class=""></i> <?php echo $row['titulo'];?> </a>               
            </li>           
        <?php }?>
        </ul>       
    </div>
</div>
</form>

See how the controller is

$id = $this->input->post('id');

$data['titulo']                   =   $this->input->post('titulo');
$data['nota']                     =   $this->input->post('nota');
$data['usuario_nome']             =   $this->session->userdata('nome_usuario');
$data['usuario_id']               =   $this->session->userdata('id');
$data['dt_alteracao'] =  strtotime(date("d-m-Y H:i:s"));            

$this->db->where('id',$id);

if($this->db->update('nota', $data)){
echo $this->db->last_query();           
die();

redirect('admin/notas/index', $data);               
    }
else
    {
redirect('admin/notas/index', $data);
}
    
asked by anonymous 16.12.2016 / 13:45

1 answer

1

Note that you just opened a form tag and added several div of class tab-pane to it with their respective inputs. You'll get HTML like this:

<form....>
     <div class="tab-pane".....>
         <input type="text" class="form-control" name="id" value="">
     </div>
     <div class="tab-pane".....>
         <input type="text" class="form-control" name="id" value="">
     </div>
      <div class="tab-pane".....>
         <input type="text" class="form-control" name="id" value="">
     </div>
     <div class="form-group">
            <div class="box-header" style="padding-left: 17px"> 
              <input type="submit" value="Salvar">
            </div>
     </div>
</form>

In this way, several inputs will be submitted with the name "id". So it was assumed the last of id 5. You need to refactor this:

You can add a form and a submit tag inside each tab-pane:

<form....>
   <div class="tab-pane".....>
       <input type="text" class="form-control" name="id" value="">
   </div>
   <input type="submit" value="Salvar">
</form>
<form....>
   <div class="tab-pane".....>
       <input type="text" class="form-control" name="id" value="">
   </div>
   <input type="submit" value="Salvar">
</form>

Now if you want to keep as is, with just one form, you need to assign dynamic name to inputs, add an input hidden that will store the id of the selected tab. It will look like this:

Assign a dynamic name to controls:

<input name=\"titulo_$row['id']\" type=\"text\" class=\"form-control\" rows=\"14\" style=\"padding: 5px; border:0px; background-color: #fff6cc; font-size: 18px;\"  placeholder=\"Título\" value=\"" . $row['titulo'] . "\">

Add a new input hidden

<form action="<?= base_url()?>admin/notas/salvar" id="" method="post" class="form-horizontal" >
  <input type="hidden" name="tab_selecionado">

Set the value of the selected tab in onclick

<a href="#<?php echo $row['id']?>" onclick="$('#tab_selecionado').val(<?php echo $row['id']?>)">" data-toggle="tab"> <i class=""></i> <?php echo $row['titulo'];?> </a> 

Controller

$id = $this->input->post('tab_selecionada');        
$data['titulo']    =   $this->input->post('titulo_'. $id);
$data['nota']      =   $this->input->post('nota_'. $id);
    
21.12.2016 / 12:13