0
I’m having an error trying to pass an ajax array to PHP and I can’t find a solution, I was based this instruction, but I couldn’t make it 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 the alert
is displayed.
But I’m not getting past the array id
to my PHP so that it works and delete the lines in the database.
delete.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);
?>
What does json_encoder function do? It seems to me to be a typo.
– jlHertel