0
I have this dynamic table
<form action="{{route ('AddDocumentosAbertura')}}" method="post" accept-charset="UTF-8" enctype="multipart/form-data">
{{ csrf_field() }}
<table >
<thead>
<th>CPF ou CNPJ</th>
<th>Porcentagem</th>
<th></th>
</thead>
<tbody id="table" class="table mg-b-0">
<tr>
<td>
<input type='text' name="cpf_ab[]" class='form-control' >
</td>
<td>
<input type='text' class="cpf form-control" name='porcentagem[]' >
</td>
<td><button type="button" class="tg88 btn btn-primary button88" id="addRowBtn">+</button></td>
</tr>
</tbody>
</table>
<button class="btn btn-primary bd-0">Enviar</button>
</form>
When the user clicks "+" it will add a new row in the table to fill in data. js that does this below:
var tbody = $('#table').children('tbody');
var table = tbody.length ? tbody : $('#table');
$('#addRowBtn').click(function(){
table.append("<tr><td><input type='text' name='cpf_ab[]' class='cpf form-control' ></td><td><input type='text' class='cpf form-control' name='porcentagem[]' ></td><td><button type='button' class='btn btn-danger delRowBtn'>-</button></td></tr>");
})
$(document.body).delegate(".delRowBtn", "click", function(){
$(this).closest("tr").remove();
});
So far so good, it’s all right, but when I send this data to my controller, it only sends the row that was manually placed inside the table (the first row). I also tested put this line that is added dynamically, I put it manually inside the table, and it worked... I’m not getting to understand why the addition of dynamic lines is not going to the server
Code that php (Standard)
$dataForm = $Request->except(['_token']);
$countcc = count($dataForm['cpf_ab']);
for ($i = 0; $i < $countcc; $i++) {
$ans = [
'cpf_ab' => $dataForm['cpf_ab'][$i],
'porcentagem' => $dataForm['porcentagem'][$i],
];
$fctr = AB_Socios::insert($ans);
}
Try sending your form via ajax.
– Marcel