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);
}
:)
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 =(
– Cheshire
Look now, my friend
– Daniel Lopes
Thank you, you helped me, I will edit the question the way it worked :)
– Cheshire