Remove checked elements in checkbox (JAVAScript, PHP)

1

I'm working on a car registration project in which I need to change my delete button that was generated for each new vehicle registered by checkbox so that more than one element can be deleted, I changed the input value to checkbox and I already adapted the functions in Javascript and PHP but I did not get a change I sent to the delete button in the menu the id's of the products to be registered, could anyone point in a direction to do this? Follow the code: PHP / HTML:

    while( $exibe = mysqli_fetch_array($sql)){

        "<h3>"; 
    echo "<tr  class=''; style='cellpadding:none;'id_automovel= (" . $exibe['id'] . ") >";
    echo"<td> <input type='checkbox' onClick='removerLinha(this)' id='delete' name='deleta[]' class='button'></td>";

    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['descricao']. "</td>" ;
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['placa']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['codigoRenavam']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['anoModelo']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['anoFabricacao']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['cor']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['km']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['marca']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['preco']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['precoFipe']. "";
    "</h3>";
    "</tr>";
}
<div class="box-side">
<h3>Ações</h3>
<ul class="mainmenu">
    <li><input class="btn-add" value="Incluir" type="button"     onClick="window.open('http://localhost/teste/templates/form.automovel.php')"     id="cd"></li> 
<li><input type="button" value="Imprimir" id="imp"></li>
<li><input type="button" value="Excluir" id="ex"></li>
</ul>
</div>

Javascript:

function removerLinha(dados){
var exclusao = $(dados).parent().parent().attr('id_automovel');
request = $.ajax({
    url: "/teste/services/automoveis.service.php?m=exclui", //excluir.php
    type: "post",
    data: "id=" + exclusao 
});

request.done(function (response, textStatus, jqXHR){
    window.location.reload();
});

request.fail(function (jqXHR, textStatus, errorThrown){
    console.error(
        "The following error occurred: "+
        textStatus, errorThrown
    );
});
};

PHP:

function removerLinha(){
$vari = $_POST['id'];
$conexao = conecta();
$sql= 'DELETE FROM automovel WHERE id =' . $vari . ' LIMIT 50';
mysqli_query($conexao, $sql);
}

Edit for the way it worked if you help someone:

PHP / HTML:

    while( $exibe = mysqli_fetch_array($sql)){

        "<h3>"; 
    echo "<tr  class=''; style='cellpadding:none;'id_automovel= (" . $exibe['id'] . ") >";
    echo"<td> <input type='checkbox' class='checkform' id='delete' value= " . $exibe['id'] . " class='button'></td>";

    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['descricao']. "</td>" ;
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['placa']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['codigoRenavam']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['anoModelo']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['anoFabricacao']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['cor']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['km']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['marca']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['preco']. "";
    echo " <td onClick =editaLinha(" . $exibe['id'] . ")>" .$exibe['precoFipe']. "";
    "</h3>";
    "</tr>";
}

JS:

function removerLinha(dados){
var exclusao = [];
console.log(exclusao);
$(".checkform:checked").each(function(){ 
    exclusao.push($(this).val());
});
console.log(exclusao);
request = $.ajax({ url: "/teste/services/automoveis.service.php?m=exclui", //excluir.php
    type: "post",
    data: "id=" + exclusao 
});

request.done(function (response, textStatus, jqXHR){
    window.location.reload();
    console.log(response);
});

request.fail(function (jqXHR, textStatus, errorThrown){
    console.error(
        "The following error occurred: "+
        textStatus, errorThrown
    );
});
};

PHP:

function removerLinha(){
$vari = $_POST['id'];
$vari = explode( ',' , $vari);
print_r($vari);

$conexao = conecta();
$sql= 'DELETE FROM automovel WHERE id =' . $vari;
mysqli_query($conexao, $sql);
}

:)

    
asked by anonymous 17.07.2017 / 20:29

2 answers

1

You will need to set the name of the checkbox as an array, example

<input type="checkbox" name="carro[]" value="idDoCarro" />

On the PHP side, just go through the then array of post / get $ _POST ['car'] and delete the values

Edition:

I did not see the excerpt in ajax, I suggest creating a new function and calling it onclick on a separate one of the lines, since the removal is in group:

function removerLinhas(dados){
var exclusao = { 'carro[]' : []};
$(":checked").each(function() {
  data['carro[]'].push($(this).val());
});
request = $.ajax({
    url: "/teste/services/automoveis.service.php?m=exclui", //excluir.php
    type: "post",
    data: exclusao 
});
    
17.07.2017 / 20:35
0

Good afternoon, I believe that the new php PDO class is more visually organized for data manipulation.

When inserting the database for inputs of type checkbox, it is necessary to create a column for each selected type, 0 for not selected or 1 for selected.

At the time of listing the bank for the html, just check if the value had 0 or 1 and then manipulate them.

    
17.07.2017 / 21:29