I'm having an error while trying to pass an array from ajax to PHP and can not find a solution, based on this statement, but I could not get it to work .
HTML:
<tr id="<?php echo $produto['idarquivo']; ?>" >
<td><input type="checkbox" name="customer_id[]" class="checkbox" value="<?php echo $produto["idarquivo"]; ?>" /></td>
<td><?php echo $produto['idarquivo']; ?></td>
<td><?php echo $produto['arquivo']; ?></td>
<td><?php echo $produto['nome']; ?></td>
</tr>
In my HTML I have a button that calls the following javascript file:
Javascript
$('#btn_delete').click(function(){
if(confirm("Deseja deletar as linhas selecionadas?"))
{
var id = [];
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length === 0)
{
alert("Selecione uma linha para deletar");
}
else
{
$.ajax({
url:'deletar.php',
method:'POST',
data:{id:id},
success:function()
{
alert ('sucesso');
}
});
}
}
else
{
return false;
}
});
Note that I put the following line alert ('sucesso');
to see if the code is working, and alert
is displayed.
But I'm not able to pass the id
array to my PHP so that it works and delete the rows in the database.
deletar.php
<?php include_once('conexao.php');
$id = $_REQUEST['id'];
print_r($id);
return json_encoder($id);
$query = "DELETE FROM arquivo WHERE idarquivo IN $id";
$resultado = mysqli_query($con, $query);
?>