Save data in BD with synfony and Javascript

0

I have some doubts when it comes to saving in my database.

I have a dynamic table to which I add the rows that I want and I put some data. I am using symfony to save the data in the database, but at the time of doing the submit it only saves me the last row that I created.

My code is this:

<div class="container-fluid">
<di class="row">
<br /><br />
<button id="add" class="btn btn-sm btn-success">Agregar</button>
<button id="del" class="btn btn-sm btn-danger">Eliminar</button>
<br /><br />

{{ form_start(form, {'method': 'post', 'action': path('precios'), 'attr': {'data-parsley-validate class': 'form-horizontal form-label-left', 'id':'formu'}}) }}
{#<form action="" method="post" 'action': path('precios')>#}
<table class="table table-hover table-condensed"  id="tabla">
    <thead>
    <tr class="info">
        <th><b>Fecha</b></th>
        <th><b>Artículo</b></th>
        <th><b>Precio Entrada</b></th>
        <th><b>Precio Salida</b></th>
    </tr>
    </thead>
    <tbody id="body">
    <tr class="fila-0" id="fila">
        <input type="text" id="contador-filas" value="1" />
        {#<td><input class="form-control" type="text"  placeholder="fecha" required /></td>#}
        <td>{{ form_widget(form.fecha, { 'attr': {'class': 'form-control ' , 'id':'t_fecha', 'size':'5px', 'name':'formu'} }) }}</td>
        {#<td><input class="form-control" type="text"  placeholder="producto" required /></td>#}
        <td>{{ form_widget(form.idProducto, { 'attr': {'class': 'form-control', 'id':'t_producto' } }) }}</td>
        {#<td><input class="form-control" type="text"  placeholder="precio entrada" required /></td>#}
        <td>{{ form_widget(form.precioEn, { 'attr': {'class': 'form-control', 'size':'5px', 'id':'t_precio_en'} }) }}</td>
        {#<td><input class="form-control" type="text"  placeholder="precio salida" required /></td>#}
        <td>{{ form_widget(form.precioSa, { 'attr': {'class': 'form-control', 'size':'5px', 'id':'t_precio_sa'} }) }}</td>
    </tr>


    <button type="submit" id="guardar" name="guardar" class="btn btn-sm btn-primary" >Guardar </button>
    <button type="reset" name="borrar" class="btn btn-sm btn-warning" >Limpiar </button>


    <script src="http://code.jquery.com/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script type="text/javascript">

        $(document).ready(function(){
            $("#add").click(function(){
                                   var tds = $("#tabla tr:first td").length;
                // Obtenemos el total de filas (tr) del id "tabla"
                var trs = $("#tabla tr").length;

                cant = $('#contador-filas').val();
                var nuevaFila = "<tr class='fila' id='fila'>";
                cant++;
                $('#contador-filas').val(cant);

                nuevaFila+="<td><input class='form-control' type='date' name='fecha["+(cant)+"]' placeholder='fecha"+(cant)+"' required /> </td>"+
                    "<td><select id='frutibundle_precioproducto_idProducto' name='frutibundle_precioproducto[idProducto]' class='form-control'><option value='4' selected='selected'>California Master 2k 26+</option><option value='5'>California Extra 2k 24+</option><option value='6'>California Plus 2k 28+</option></select></td>"+
                    "<td><input class='form-control' id='frutibundle_precioproducto_precioEn' type='text' name='frutibundle_precioproducto[precioEn]' placeholder='precio_en"+(cant)+"' required /> </td>"+
                    "<td><input class='form-control' id='frutibundle_precioproducto_precioSa' type='text' name='frutibundle_precioproducto[precioSa]' placeholder='precio_sa"+(cant)+"' required /> </td>";


                nuevaFila += "</tr>";
                 $("#tabla").append(nuevaFila);
            });
            /**
             * Funcion para eliminar la ultima columna de la tabla.
             * Si unicamente queda una columna, esta no sera eliminada
             */
            $("#del").click(function(){
                var trs=$("#tabla tr").length;
                if(trs>2)
                {
                    cant--;
                    $('#contador-filas').val(cant)
                    $("#tabla tr:last").remove();

                }
            });
        });
    </script>
    </tbody>
</table>
{{ form_end(form) }}
    
asked by san_mm 29.05.2018 в 09:48
source

0 answers