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);
}
:)