can you share your code?
Laravel 5.1 - Controller dont get input fields generated by Javascript
I have a Controller, a View and a JS file. My JS file generate a table row with two input and three select fields every time I click a button. This table row is appended to the table in the view with jQuery append() method.
When I press submit, if I do print_r($request), the controller prints out all the array, with the except of the JS generated fields. They are invisible to the Laravel methods.
How can I get the data of this fields?
Controller
$cliente = Cliente::findOrFail($id);
$input = $request->all();
print_r($input);
HTML
{!! Form::model($cliente, [
'method' => 'POST',
'id' => 'profile_form',
'route' => ['perfilcliente', $cliente->id]
]) !!}
<table id="estudios_tabla" class="table table-flipscroll table-responsive table-hover table-striped" data-newindex="<?php print $last_estudio_index; ?>" data-index="<?php print $last_estudio_index-1; ?>">
<thead>
<tr>
<th>Operacións</th>
<th>Ano</th>
<th>Nivel de Estudios</th>
<th>RIC</th>
<th>Especialiadad</th>
<th>Satisfacción</th>
</tr>
</thead>
<tbody id="body_estudios">
@foreach($estudios_array as $estudio_cursado)
<tr id="estudio_line_<?php print $estudio_cursado['id_estudio']; ?>">
<td width="10%" style="text-align: center;">
<a href="#" title="Borrar fila" id="remove_row_<?php print $estudio_cursado['id_estudio']; ?>" class="btn btn-danger tooltips" style="clear: both;" data-remove="estudio_line_<?php print $estudio_cursado['id_estudio']; ?>">
<i class="fa fa-ban"></i>
<span></span>
</a>
</td>
<td width="10%">
{!! Form::text('ano_estudio_'.$estudio_cursado['id'], $estudio_cursado['ano'], ['class' => 'form-control', 'id'=>'ano_estudio_'.$estudio_cursado['id']]) !!}
</td>
<td width="20%">
{!! Form::select('niveles_estudios_'.$estudio_cursado['id'],
$items_estudios,
$estudio_cursado['id_estudio'],
['class' => 'form-control'])
!!}
</td>
<td width="10%">
{!! Form::select('ric_estudios_'.$estudio_cursado['id'],
['r'=>'Realizado', 'i'=>'Iniciado','c'=>'En curso'],
$estudio_cursado['ric'],
['class' => 'form-control', 'id'=>'ric_estudios_'.$estudio_cursado['id']])
!!}
</td>
<td width="30%">
{!! Form::text('especialidad_estudio_'.$estudio_cursado['id'], $estudio_cursado['especialidad'], ['class' => 'form-control']) !!}
</td>
<td width="20%">
{!! Form::select('satisfaccion_estudio_'.$estudio_cursado['id'],
['baja'=>'Baixa', 'media'=>'Media','alta'=>'Alta'],
$estudio_cursado['satisfaccion'],
['class' => 'form-control'])
!!}
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="form-group">
<p style="height: 1px;"> </p>
<button class="btn btn-primary pull-right" id="agregar_estudio" style="margin: 0 15px;">Agregar formación cursada</button>
<p style="height: 1px; clear: both;"> </span>
</div>
{!! Form::close() !!}
Javascript
$('body').on('click', '#agregar_estudio', function(){
event.preventDefault();
var indice = $('#estudios_tabla').data("index");
var newindice = $('#estudios_tabla').data("newindex");
$('#estudios_tabla').data("newindex", newindice+1);
var cloned_estudios = select_estudios;
var cloned_ric = select_ric;
var cloned_satis = select_satisfaccion;
var outputHtml = "<tr id='estudio_line_"+newindice+"'>";
outputHtml = outputHtml+" <td width='10%' style='text-align: center;'>";
outputHtml = outputHtml+" <a href='#' title='Borrar fila' id='remove_row_"+newindice+"_estudios' class='btn btn-danger tooltips tooltips-right' style='clear: both;' data-remove='estudio_line_"+newindice+"'>";
outputHtml = outputHtml+" <i class='fa fa-ban'></i>";
outputHtml = outputHtml+" <span>Borrar (sen gardar)</span>";
outputHtml = outputHtml+" </a>";
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" <td width='10%'>";
outputHtml = outputHtml+" <input type='text' value='' name='ano_estudio_"+newindice+"' class='form-control' id='ano_estudio_"+newindice+"' />";
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" <td width='20%'>";
outputHtml = outputHtml+" "+cloned_estudios.replace("niveles_estudios","niveles_estudios_"+newindice);
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" <td width='10%'>";
outputHtml = outputHtml+" "+cloned_ric.replace("ric_estudios","ric_estudios_"+newindice);
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" <td width='30%'>";
outputHtml = outputHtml+" <input type='text' value='' class='form-control' name='especialidad_estudio_"+newindice+"' id='especialidad_estudio_"+newindice+"' />";
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" <td width='20%'>";
outputHtml = outputHtml+" "+cloned_satis.replace("satisfaccion_estudios","satisfaccion_estudio_"+newindice);
outputHtml = outputHtml+" </td>";
outputHtml = outputHtml+" </tr>";
$('#body_estudios').append(outputHtml);
$('#remove_row_'+newindice+'_estudios').click(borrar_fila);
});
Response
Array
(
[_token] => RzYxF9D2HNeo2aJt0e9sUtlhMhXbBxMv6dT23ieo
[nombre] => Susana
[apellidos] => Rodríguez Torrón
[foto] => susana_rodriguez_torron.png
[dni] => 36113503F
[fecha_nacimiento] => 25/09/1984
[direccion] => Zamora 84 Bajo - 362020 Vigo (España)
[telefono] => 902002271
[movil] => 684006939
[frecuencia] => Diario
[email] => susana@gmail.com
[estado_civil] => Soltera
[nacionalidad] => Española
[colectivos_1] => colectivos_1
[colectivos_4] => colectivos_4
[colectivos_5] => colectivos_5
[colectivos_6] => colectivos_6
[otros_datos] => Ninguno
[sexo] => 1
[minusvalia] => 1
[minus_porcentaje] => 45
[minus_psiquica] => minus_psiquica
[minus_limitaciones] => Ninguna
[percibe_prestaciones] => 1
[presta_1] => presta_1
[prest_duracion] => 12
[prest_inicio] => 12/09/2014
[prest_importe] => 867.23
[prest_tiempo] => corta
[fam_esposo] => 1
[fam_esposo_edad] => 32
[fam_hijo] => 2
[fam_hijo_edad] => 12 y 13
[fam_padres] => 2
[fam_padres_edad] => 56, 50
[fam_abuelo] => 1
[fam_abuelo_edad] => 89
[fam_otros] => 1
[fam_otros_edad] => 18
[carnet_1] => carnet_AM
[coche_propio] => 1
[xornada_laboral] => completa
[nocturnidade] => 0
[quendas] => 0
[quenda_porque] =>
[ha_trabajado] => 0
[gusto] => 0
[gusto_porque] =>
[otros] =>
[retribucion_salarial] => poco
[politica_1] => politica_1
[politica_3] => politica_3
[politica_4] => politica_4
[politica_6] => politica_6
[pol_observaciones] =>
[ano_estudio_1] => 2002
[niveles_estudios_1] => 4
[ric_estudios_1] => r
[especialidad_estudio_1] => Desarrollo de aplicaciones informáticas
[satisfaccion_estudio_1] => alta
[ano_estudio_2] => 2008
[niveles_estudios_2] => 7
[ric_estudios_2] => r
[especialidad_estudio_2] => Ingeniería informática superior
[satisfaccion_estudio_2] => baja
[estudios_destrezas] => 245
[ano_formacion] => 2002
[nombre_formacion] => 2
[entidade] => Academia Postal
[duracion] => 2
[formacion_destrezas] => 789
[nivel_informatica] => medio
[frecuencia_ordenador] => Todos os días
[ordena_casa] => 1
[internet_ordena] => 1
[internet_movil] => 1
[redes_1] => redes_1
[redes_3] => redes_3
[redes_4] => redes_4
[frecuencia_redes] => A diario
[sms_1] => sms_1
[sms_3] => sms_3
[fixo_cv] => Un amigo
[nombre_programa] => 3
[ano_programa] => 2012
[nivel_programa] => alto
[tipo_aprendizaje] => adquisicion
[nombre_idioma] => 1
[nivel_idioma] => bajo
[tipo_aprendizaje_idioma] => formacion
[facilidade_idioma] => Pouca
[experiencia_posto] => Experiencia adquirida en el puesto 2
[tarefa_1_exp_1] => Organización de pedidos
[satisfaccion_experiencia_1_exp_1] => alta
[tarefa_2_exp_1] => Contabilidad
[satisfaccion_experiencia_2_exp_1] => baja
[tarefa_3_exp_1] => Recursos Humanos
[satisfaccion_experiencia_3_exp_1] => alta
[tarefa_4_exp_1] => Programación de Agenda
[satisfaccion_experiencia_4_exp_1] => media
[tarefa_5_exp_1] => Entrevistas de personal
[satisfaccion_experiencia_5_exp_1] => baja
[empresas] => 2
[actividad] => Administrativo
[ambito] => Provincial
[fin_contrato] => voluntario
[fecha_fin] => 2012-02-22
[otro] => Embarazo
[bolsa_emprego] => 1
[cono_aemos] => 1
[paxinas_inter] => 1
[traballo_temporal] => 1
[que_bolsa] => Sergas
[que_aemos] => Mostrouma un amigo
[que_paxinas_inter] => Infojobs
[que_empresas_temporal] => Manpower
)
a lot of code :)
with console code inspect, does newindice value updates correctly? I mean, if you with javascript add new row, it has correct index? Perhaps it conflict with existing?
one tip: you can use array in HTML form like:
<input type="text" name="estudios[1]title" />
<input type="text" name="estudios[1]content" />
In this way it would be easier to proccess response data
It is only a small portion of my code, its a complete CRM (32 db tables and growing, a lot of models, controllers, multiple layouts).
I revised the newindice generation with firebug and the value updates correctly.
When there no records, the table appears empty (as it has to be), and clicking, the new row appends with newindice = 1. When there are records, I take the last db record index as value for the code, then I go adding 1 to newindice. I tested, and the code runs perfect. Except of that print_r, which dont catch the javascript rows autogenerated.
I will tell you my purpose, if you know another way to do it better, then I can change the code. We have a client with some studies. Its a showing all the data. Before saving the client, the user must be capable of adding new studies (as long as he can add languages, informatic applications, and more, to the cliente). And, when our user submits the form, all the data is saved.
Hi Dear Javi Adagal, I wanted to use your good sample in my project. it is exactly what I need. would you please explain what is
var cloned_estudios = select_estudios;
in javascript file or just send me the full source code of these classes. if you send the source code for me I can implement my page faster. this is my email address: amin.dehghani@yahoo.com
Thanks in advance, Amin
Please or to participate in this conversation.