I have a table with sorted data in rows and columns. A column is for delete , with checkboxs selected, when we press the DELETE button and a " confirm () "to confirm the deletion or be canceled.
In the second question I have the problem: in the cancellation. If I cancel, the checkboxs are not erased or deactivated ... I can not get it!
HTML / PHP code:
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Panel del administrador</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="js/desmarcar_checkboxs.js"></script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" name="tabla_datos_cabana" id="tabla_datos_cabana" method="POST">
<div id="mostrar_cabanas">
<table class="table table-striped" id="tabla" name="tabla" width="600" border="2" cellspacing="3" cellpadding="3" style="font-size: 10pt">
<center>
<tr>
<thead style="background-color:#A9F5A9">
<td width=\"150\">
<font face="verdana" color="blue"><b><center>Eliminar</center></b></font>
</td>
</thead>
</tr>
<?php
$datos = BD::obtenerCabanas();
foreach($datos as $cabana){
echo "<tr>";
echo "<td width=\"150\"><center><input type='checkbox' name='marcados[]' id='marcados[]' value=".$cabana->getIdcabana()."></center>";
echo "</tr>";
}
?>
</center>
</table>
<!-- Botón ELIMINAR cabaña/s -->
<div class="boton_eliminar" class="table-responsive" align="left">
<font face="verdana">
<b><input type="submit" style="width:200px; height:28px;" name="eliminar_cabanas" id="eliminar_cabanas" onclick="return confirm('¿Deseas realmente eliminar estas cabañas?');" value="Eliminar cabañas" /></b>
</font>
</div>
<?php
//Si pulsamos el botón "Eliminar cabañas"...
if(isset($_POST['eliminar_cabanas'])){
if(empty($_POST['marcados'])){
echo "<h4><center>No se ha seleccionado ninguna cabaña.</center></h4>";
}else{
foreach($_POST['marcados'] as $valor){
//Nos conectamos a la base de datos.
$conexion = mysqli_connect("localhost", "root", "root", "osmarrural");
//Realizamos la consulta.
$sql = sprintf("DELETE FROM cabanas WHERE idcabana='%d'", $valor);
$resultado = mysqli_query($conexion, $sql);
}
echo "<meta http-equiv=\"refresh\" content=\"0; URL=menu_administrador.php\">";
}
}
?>
</div>
</form>
</body>
</html>
File call code uncheck_checkboxs.js:
//Seleccionamos el botón.
var btn = document.getElementById('eliminar_cabanas');
//Asignamos el evento click.
btn.onclick = function(e){
//Obtenemos y asignamos el valor de retorno de confirm: true o false.
let option =confirm('¿Deseas realmente eliminar estas cabañas?');
if(!option){ //Si es falso...
//Seleccionamos todos los checks.
let checks = document.querySelectorAll('input[type="checkbox"]');
//Iteramos sobre estos.
checks.forEach(function(el){
//Asigamos el atributo a false.
el.checked = false;
});
}
}
If I get the inspector with F12, I get the following error:
Uncaught TypeError: Cannot set property 'onclick' of null
at desmarcar_checkboxs.js:5
Error image: