Remove checked items in the checkbox (Javascript, PHP)

Asked

Viewed 304 times

1

I’m working on a car registration project where 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 value of the input to checkbox and already adapted the functions in Javascript and PHP but I could not get a change that I sent to the delete button in the menu the id’s of the products to be registered, could anyone point out a direction to do this? It follows 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 to 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);
}

:)

2 answers

1


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

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

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

Editing:

I did not see the section in ajax, I suggest creating a new function and calling it in onclick in another separate line, 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 
});
  • So, the name is already in Array, the problem is in calling the function with the delete button, instead of deleting as soon as the checkbox is checked, but so far everything I tried did not work and I have no idea which is the correct way =(

  • Look now, my friend

  • Thank you, you helped me, I will edit the question the way it worked :)

0

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

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

At the time of listing the database to html, only check whether the value has 0 or 1 and so manipulate them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.