My problem is that I have a table of products and the number of pieces chosen, but I have the option that the pieces are changed by a select
and consequently the subtotal is changed, and the total of all the buy, but it only works with the first data shown by the while
, but does not change it with the others
This is where I show the aggregated data and here I have the select
preloaded with the number of pieces selected, and can also be modified from select
<table class="table table-hover table-condensed table-bordered">
<tr>
<th>Producto</th>
<th>Precio</th>
<th>Piezas</th>
<th>Subtotal</th>
<th>Eliminar</th>
</tr>
<?php foreach ($p as $registros ): ?>
<?php if ($registros[piezas]!=0 and $registros[subtotal]!=0) {
$datos=$registros[id];
$total=$total + $registros['subtotal'];
?>
<input type="hidden" name="" value="<?php echo $registros[id]; ?>" id="id_">
<tr>
<th><?php echo $registros[producto]; ?></th>
<th><?php echo $registros[precio]; ?></th>
<th><select class="" name="" id="piezas">
<option value="<?php echo $registros[piezas]; ?>"><?php echo $registros[piezas]; ?></option>
<?php if ( $registros[piezas]!="1" ): ?>
<option value="1">1</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="2" ): ?>
<option value="2">2</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="3" ): ?>
<option value="3">3</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="4" ): ?>
<option value="4">4</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="5" ): ?>
<option value="5">5</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="6" ): ?>
<option value="6">6</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="7" ): ?>
<option value="7">7</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="8" ): ?>
<option value="8">8</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="9" ): ?>
<option value="9">9</option>
<?php endif; ?>
<?php if ( $registros[piezas]!="10" ): ?>
<option value="10">10</option>
<?php endif; ?>
</select> </th>
<th><?php echo $registros[subtotal]; ?></th>
<th> <button class="btn btn-danger " value="<?php echo $datos ?>"onclick="preguntarpedido(<?php echo $datos; ?>)"><i class="fas fa-minus-circle"></i></button></th>
</tr>
<?php
} ?>
<?php endforeach; ?>
<tr>
<td colspan="6">TOTAL: <?php
echo $total;
?></td>
</tr>
this function the charge until the end, and is responsible for changing the pieces
<script type="text/javascript">
$(document).ready(function() {
$('#piezas').change(function(){
$.ajax({
type:"post",
data:'piezas=' + $('#piezas').val()+
'&id=' + $('#id_').val(),
url:'../php/pedidoscambiar.php',
success:function(r){
$('#tabla').load('tablaagregados.php');
}
});
});
});
</script>