1
I have to edit a record series, the same are listed in a table (see):
This is the form of the image above:
<div class="control-group">
<table class="table table-bordered ">
<thead>
<tr style="backgroud-color: #2D335B">
<th>Nome do Envolvido</th>
<th>RG</th>
<th>Envolvimento</th>
<th>Opções</th>
</tr>
</thead>
<tbody>
<?php if (!$envolvidos) { ?>
<td colspan="5">Ainda sem envolvidos</td>
<?php } else {
foreach ($envolvidos as $r) { ?>
<tr>
<td style="text-align: center">
<input type="hidden" id="id_e" name="id_e"
value="<?php echo set_value('id', $r->id); ?>">
<input class="span9" type="text" id="nome_e" name="nome_e"
value="<?php echo set_value('nome', $r->nome); ?>">
</td>
<td style="text-align: center">
<input class="span6" type="text" id="rg_e" name="rg_e"
value="<?php echo set_value('rg', $r->rg); ?>">
</td>
<td style="text-align: center">
<select style="width: 120px" id="envolvimento_e" name="envolvimento_e"
value="<?php echo set_value('envolvimento', $r->envolvimento); ?>">
<option
value="SOLICITANTE" <?php if ($r->envolvimento == 'SOLICITANTE') echo 'selected' ?>>
Solicitante
</option>
<option
value="AUTOR" <?php if ($r->envolvimento == 'AUTOR') echo 'selected' ?>>
Autor
</option>
<option
value="VITIMA" <?php if ($r->envolvimento == 'VITIMA') echo 'selected' ?>>
Vítima
</option>
<option
value="TESTEMUNHA" <?php if ($r->envolvimento == 'TESTEMUNHA') echo 'selected' ?>>
Testemunha
</option>
</select>
</td>
<td style="text-align: center">
<button type="button" id="enviar" class="btn btn-primary btn-lg"> Editar
</button>
</td>
</tr>
<?php }
} ?>
</tbody>
<thead>
<tr style="backgroud-color: #2D335B">
<th>Nome do Envolvido</th>
<th>RG</th>
<th>Envolvimento</th>
<th>Opções</th>
</tr>
</thead>
</table>
And I’ve tried putting each tr
within a form
, the problem that editing only works for the first item of table
the others does not fire the ajax that sends to the controller. See the ajax:
jQuery('#enviar').click(function () {
var id = $('#id_e').val();
var nome = $('#nome_e').val();
var rg = $('#rg_e').val();
var envolvimento = $('#envolvimento_e').val();
$.post("<?php echo base_url(); ?>index.php/sbp/editarEnvolvido",
{'id': id, 'nome': nome, 'rg': rg, 'envolvimento': envolvimento},
function (data) {
alert(data);
}, "html");
});
For better understanding here is the controller function that does the editing in the database:
function editarEnvolvido(){
$id = $_POST['id'];
$nome = $_POST['nome'];
$rg = $_POST['rg'];
$envolvimento = $_POST['envolvimento'];
if((strcmp($id,"") != 0) && (strcmp($nome,"") != 0) && (strcmp($rg,"") != 0)){
$data = array(
'nome' => $nome,
'rg' => $rg,
'envolvimento' => $envolvimento
);
if ($this->sbp_model->edit('envolvidos', $data, 'id', $id) == TRUE) {
echo 'Salvo com sucesso!';
} else {
echo 'Erro ao tentar salvar!';
}
} else {
echo 'Nome e/ou RG deve ser informado!';
}
}
I would like to at least understand what is happening that the other lines does not fire the ajax, and accept additional suggestions to solve my problem. Grateful from the start!
When you said you put each form on a tr I imagine it only worked because you used the same id on the button
#enviar
which should work, can often be errors like this, even id for the form and tals– Marcelo Diniz
I get it, you have a suggestion for me to troubleshoot this record editing issue?
– Julio Cesar da Silva Barros
you can take the
$r->id
put as attribute in button and shape and make a serialize picking by id. I’m half run here, but it’s this way, half you already have there– Marcelo Diniz
Thanks man, I’ll try here! Thanks!
– Julio Cesar da Silva Barros