I am doing a crud of a table with a foreach to show the data from the database, the table would be the following:
I'm doing the deletion confirmations with a SweetAlert, that when confirming, the row is deleted. The problem would be that when I want to capture the value of <?php echo $row['incidencia_nombres'] ?>
in my SweetAlert, it would have to show me the name of the row that I want to delete, but it is showing me the last data.
Here I wanted to eliminate row 3, from which it would have to show me the name "Daniel" and not "Hector", being the last name of the column.
This error would also happen to me when redirecting the href
, since I want to capture the id
one of the row that I want to delete and send it to my php file so that it proceeds to delete using the one that id
it will pick up, but it would delete the last row.
I attach the code <tbody>
of my table table, where I show the data and call the Sweet Alert through a javascript function.
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">Acción</th>
<th class="text-center">Nombres</th>
<th class="text-center">Apellidos</th>
<th class="text-center">DNI</th>
<th class="text-center">Teléfono</th>
<th class="text-center">Dirección</th>
<th class="text-center">Medicina</th>
<th class="text-center">Fecha</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($result as $row) {
?>
<tr>
<td style="color: black;" class="text-center"><?php echo $i++?></td>
<td class="text-center">
<a target="_blank" href="editincidencia.php?id=<?php echo $row['incidencia_id'] ?>"><button type="button" class="btn btn-xs btn-primary"><i class="fa fa-pencil"></i></button></a>
<a target="_blank" href="invoiceprintIncidencias.php?id=<?php echo $row['incidencia_id'] ?>"><button type="button" class="btn btn-xs btn-success"><i class="fa fa-print"></i></button></a>
<a onclick="removeIncidencias()"><button type="button" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button></a>
</td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_nombres'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_apellidos'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_dni'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_telefono'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_direccion'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_nombre_medicina'] ?></td>
<td style="color: black;" class="text-center"><?php echo $row['incidencia_fecha'] ?></td>
</tr>
<script>
function removeIncidencias() {
Swal.fire({
title: '¿Eliminar incidencia?',
text: "Procederás a eliminar a <?php echo $row['incidencia_nombres'] ?>, recuerda que... ¡Esto es irreversible!", //El error ocurriría aquí
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: '¡Sí, eliminar!',
cancelButtonText: "Cancelar",
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'¡Eliminado!',
'Incidencia eliminada correctamente.',
'success'
)
window.location.href = "php_action/removeIncidencias.php?id=<?php echo $row['incidencia_id']?>"; //El error ocurriría aquí también
}
});
};
</script>
</tbody>
<?php
}
?>
I am putting the Sweet Alert inside the foreach so that it supposedly captures the value of the row that I want to delete, but it is always taking the last one, I would greatly appreciate your help.
From what I understand in your code you are defining in each iteration the function removeIncidencias()
At the end of your foreach iterations, the last record is Hector 's, so regardless of which record you select, the function is defined to show and delete this record.
I recommend you to make more use of JavaScript, the solution to your problem is the following
to the label A where you do the onClick to the removeIncidencias() function I added two parameters, which are the id of the incident and the name you want to display
once you finish the foreach and outside of it so that it is only defined once we will put your function
The change I made in your function was to add the two parameters that it will receive once you click on the A label. Later, instead of printing the data with php, the ShowName parameter is concatenated to the string where you ask the user if they want to delete the incident, and if confirmed, the idIncidencia parameter is concatenated to the string where you change the url
As I was telling you, I recommend that you study and make more use of javascript since this way of solving this problem is not the best, it is just an easy way and without you having to modify your code a lot