I am doing an exercise in which you must select the months you want to pay by selecting the corresponding checkbox.
The table is generated from a query to the database. Therefore, this can have one or several records so I do not know how to assign a identificador
to the row from which to take the information. The intention of all this is that before performing operations on the server side the program shows in a modal the summary of what is going to be paid. (Img 1).
id fecha costo seleccione 1 Enero 35.50 $ ☐ 2 Febrero 50.00 $ ☐ 3 Marzo 20.50 $ ☐ 4 Abril 75.00 $ ☐ 5 Mayo 10.00 $ ☐ 6 Junio 15.00 $ ☐
It occurs to me that I could do the following in id
of <td>
:% <td id='fecha[]'><?echo $fila['fecha']?></td>
with this the id had an identifier different from the others, but from there with javascript I do not know how to go through that% fecha[]
to access the information it has.
The program code is as follows.
$datos = $con->query("SELECT * FROM pago");
if(isset($_POST['btnEnviar'])){
if(!empty($_POST['fecha'])){
?>
<div class="alert alert-success">
Se a realizado el pago de :
<?
foreach ($_POST['fecha'] as $key) {
echo '<ul><li>'.$key . '</li></ul>';
}
?>
</div>
<?
}else{
?>
<div class="alert alert-danger">
No a seleccionado nada!
</div>
<?
}
}
?>
<div class="container">
<div class="col-lg-5">
<form class="" action="<?echo $_SERVER['PHP_SELF']?>" method="post" id="formulario">
<table class="table table-bordered table-hover" >
<thead>
<tr class="warning">
<th>id</th>
<th>fecha</th>
<th>costo</th>
<th>seleccione</th>
</tr>
</thead>
<tbody>
<?
if(!empty($datos)){
while($fila = mysqli_fetch_array($datos)){
?>
<tr>
<td><?echo $fila['id']?></td>
<td><?echo $fila['fecha']?></td>
<td><?echo $fila['costo']?> <b>$</b></td>
<td>
<input type="checkbox" name="fecha[]" value="<?echo $fila['fecha']?>">
</td>
</tr>
<?
}
}
?>
</tbody>
</table>
<div class="text-center">
<span id="btn" class="btn btn-success" data-target="#modal-pagar" data-toggle="modal">Pagar</span>
</div>
<div class="modal fade modal-default" id="modal-pagar" tabindex="-1" role="dialog" aria-labelledby=""
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="">Resumen de pago</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<span class="btn btn-default">Cancelar</span>
<button type="submit" name="btnEnviar" class="btn btn-success">Pagar</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
btn = $('#btn');
mes = "<input type='text' value='Abril' class=''>";
costo = "<input type='text' value='75$' class=''>";
total= "<h4>Total a pagar <span class='text-success'>75 $</span></h4>";
btn.click(function(){
$('.modal-body').append(mes,costo,total);
});
});
</script>
</body>
</html>
It may be easy but I have no idea how to capture the values of the row and present them in the modal.