Passing Correct Form Ubmit Parameters

Asked

Viewed 773 times

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 tab clicked and display it in an input and texarea.

And it is working and loading correctly, as link this link.

Now I need that when changing anything and Submit done, the change be saved in the bank.

However, when I send the form, regardless of the ID I’m changing, I’m getting the following feedback:

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 the comic.

See below for 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>

Check out the controller

$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);
}

1 answer

1


Note that you just opened a tag formand within it added several div class tab-pane with their respective inputs. You will have an HTML more or less 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>

This way you will be submitted several inputs with the name "id". So we assumed the last of id 5. You need to refactor this:

You can add a tag form and a submit 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 it as it is, with only one form, you need to assign dynamic names to inputs, add an input hidden which 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 Hidden input

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

Set the value of the tab selected 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);
  • Face, perfect. I’ll try the code better, but with that the problem has been solved. Thanks for the help!!!

Browser other questions tagged

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