Update value within a Foreach

2

please help me, I need to update the result of my total every time I change the amount

                                <script type="text/javascript">
                                    function operacion(field) {
                                        //Obtengo el formulario
                                        var form = field.parentNode; 
                                        //Obtengo el valor del campo 1
                                        var numero1 = form.cantidad.value; 
                                        //Obtengo el valor del campo 2          
                                        var numero2 = form.precio.value;
                                        //Hago el calculo y se lo asigno al campo de texto correspondiente          
                                        form.total.value = ( numero1 * numero2 );                                                                              
                                        console.log(form.total.value);
                                    }    
                                </script>
<?php
    $totalProducto=0;
    foreach ($sql_Producto as $itemListarProductoCarrito):
    $preci = $itemListarProductoCarrito['costo_promedio_producto'];
?>
<form>
     <table  class="table table-bordered">                                                                   
      <tr>
      <a href="eliminardelcarro.php?id=<?php echo $c["product_id"]; ?>" class="fa fa-fw fa-minus-circle" data-toggle="tooltip" title="Eliminar"></a>
    </tr>                                                                                                                          
    <tr><br>
    <!--aqui le aumento para la cantidad para poder modificar si el ususario lo desea-->
    <label>Cantidad</label>
    <select name="cantidad" style="width:100px;" class="form-control" onchange="operacion(this)" >
        <option value="<?php echo $c["q"]; ?>"><?php echo $c["q"]; ?></option>
        <option value=""></option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>                                                                        
    </tr> 
    <tr><br>
    <td><label>Precio Unitario</label></td>
    <td><input align="right" style="width:100px;" class="form-control" type="text" name="precio" value="<?php echo $preci;/*recibo mi precio*/ ?>"  onchange="operacion(this)"  readonly></td>
    </tr>
    <tr>
        <td><label>Total</label></td>
        <td><input align="right" style="width:100px;" class="form-control" type="text" name="total" value="<?php echo '$' . $total = $c["q"] * $preci; /*aqui multiplico la cantidad enviada * por el precio*/?>"readonly></td>
        <?php $totalProducto += $total;/*sumo todos los valores totales*/ ?>
    </tr>
</table>

     </form>
 <?php
 endforeach;
?>

until everything works fine since I add all the values within my foreach Now I need to add the values but now these with a SELECT to change the amount since the amount $ c ["q"] sends me from another form but I can not with the new amount since I would be containing it with the name="total" I think it would be something to do something but I can not think of it as

    
asked by Jhon Di 28.09.2018 в 18:44
source

0 answers