Deleting records marked with checkbox

2

I wanted to be able to delete multiple records from a table just by selecting the records using checkbox , same as Gmail, I select the emails that I want to delete or mark as read. Because when I need to delete some registry I still have to delete one at a time.

I'm very lay in JS , until I was able to show in an alert the array with the ids that the user selected, but I do not know how to execute a sql statement to delete the marked records, the idea would be to delete it as soon as the user to click "OK" within the alert.

I've been searching the internet but I still have doubts, some examples used ajax.

Sample Code

<script type="text/javascript">
    function coletaDados() {
        var ids = document.getElementsByClassName('editar');
        coletaIDs(ids);
    }

    function coletaIDs(dados) {
        var array_dados = dados;
        var newArray = [];
        for (var x = 0; x <= array_dados.length; x++) {
            if (typeof array_dados[x] == 'object') {
                if (array_dados[x].checked) {
                    newArray.push(array_dados[x].id)
                }
            }
        }
        if (newArray.length <= 0) {
            alert("Selecione um pelo menos 1 item!");
        } else {
            alert("Clique em OK para confirmar a exlusão do(s) registro(s) : [ " + newArray + " ]");
        }
    }
</script>

<?php
// Variáveis declaradas somente para ilustrar o exemplo

$id1 = "1";
$id2 = "2";
$id3 = "3";
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Tabela de Exemplo</title>
    </head>
    <body>
        <table border="1" width="10%">
            <tr>
                <td>Registros</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="editar" id="<?php echo "$id1"; ?>"> A</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="editar" id="<?php echo "$id2"; ?>"> B</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="editar" id="<?php echo "$id3"; ?>"> C</td>
            </tr>
            <tr>
                <td colspan="2"><button onclick="coletaDados()" class="btn btn-primary" style="width:100%">Excluir</button></td>
            </tr>
        </table>
    </body>
</html>
    
asked by anonymous 19.10.2017 / 14:28

1 answer

1

This code sends by ajax the id's that you want to delete:

  

See comments

function coletaDados() {
    var ids = document.getElementsByClassName('editar');
    coletaIDs(ids);
}

function coletaIDs(dados) {
    var array_dados = dados;
    var newArray = [];
    for (var x = 0; x <= array_dados.length; x++) {
        if (typeof array_dados[x] == 'object') {
            if (array_dados[x].checked) {
                newArray.push(array_dados[x].id)
            }
        }
    }
    if (newArray.length <= 0) {
        alert("Selecione um pelo menos 1 item!");
    } else {
      var ajax = new XMLHttpRequest();

      ajax.open("POST", "nomeDaPagina.php", true); //definir o nome do arquivo que recebe os dados via POST.
      ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");


      ajax.send('ids=' + newArray);

      ajax.onreadystatechange = function() {

        if (ajax.readyState == 4 && ajax.status == 200) {

          var data = ajax.responseText;
          // seu código para apagar os ids da página
          alert(data); // retorno da página PHP.
        }
      }
    }
}

The PHP page that will process the ID's to delete the records must be expecting the ids parameter by the POST method as below:

$ids = $_POST['ids'];

If you are using PDO you can delete it this way:

$ids = $_POST['ids'];
require_once('db.php'); //chama seu arquivo de conexão com o banco de dados

$exclui = $con->prepare("DELETE FROM suaTabela WHERE id in ( :ids )");
$exclui->bindValue(":ids", $ids);
$exclui->execute();
echo "ID's apagados: ". $ids; //retorno que será enviado para o alert.
    
20.10.2017 / 04:18