Delete checkboxs when canceling confirm ()

0

I have a table with data and some checkboxs corresponding to their respective row. If I select one or several checkboxs and click Delete , you will ask me in a " confirm () " if I want to delete them. If I click on " Accept ", it deletes them correctly and if I deny the question, that is, I choose the option " Cancel " cancels the operation but keeps the checkbox active previously chosen.

I wish that by pressing " Cancel ", the active checkboxs will be deleted.

HTML Code:

<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 Code:

<?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=panel_administrador.php\">";
    }
}
?>
    
asked by omaza1990 06.01.2018 в 01:58
source

1 answer

1

This becomes complicated if you continue assigning the listeners and the functions to be executed in these elements directly from the HTML which I do not think is entirely correct.

When canceling or pressing the% key% of%%% will return ESC said function, this return value could validate to assign the attribute confirm of false to false. (I would not see PHP necessary for this, unless I'm missing some detail)

Ejm

//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){ // Sí 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; 
    });
  }
}
<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"  value="Eliminar cabañas" /></b>
     </font>
</div>
<input type="checkbox" name="option" value="2">
<input type="checkbox" name="option" value="3">
<input type="checkbox" name="option" value="4">
<input type="checkbox" name="option" value="5">
    
answered by 06.01.2018 в 04:10