Error converting Ajax Array to Array in PHP

Asked

Viewed 92 times

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.

2 answers

1


The best way would be to make a json and pass to data: {id:id} so in PHP it was just decode and loop the delete command. But one possible rs solution is the below. It concatenates each ID and in PHP it makes each ID look like an array item and executes the loop.:

JS

$('#btn_delete').click(function(){

  if(confirm("Deseja deletar as linhas selecionadas?"))
  {
    var id = '';

    $('.checkbox:checked').each(function(){
       id += $(this).val()+',';
    });

    if(id == '' || id == ',') 
    {
        alert("Selecione uma linha para deletar");
    }
    else
    {
        $.ajax({
             url:'deletar.php',
             type:'POST',
             data:{ids:id},
             success:function()
             {
                 alert ('sucesso');
             }

        });
    }

  }
  else
  {
    return false;
  }
});

PHP

<?php include_once('conexao.php');

$ids = $_POST['ids'];

print_r($ids);

$exp = explode(",", $ids);

foreach($exp as $id){

$query = "DELETE FROM arquivo WHERE idarquivo = '$id'";
$resultado = mysqli_query($con, $query);

}
  • only a complement, not part of the question but if you can help me. There is how I use a .fadeOut('slow'); in tr table to cause a table effect being removed?

  • How is the <tr> of your table? How to play a <tr> entire here ?

0

Try using POST instead of REQUEST. See if it works! In the request ajax change the method:'POST' for type: 'POST' and do your php like this and change the ajax too!

$.ajax({
             url:'deletar.php',
             method:'POST',
             data:{id:JSON.stringify(id)},
             success:function()
        {
                     alert ('sucesso');
                 }

            });

PHP

<?php include_once('conexao.php');

$id = json_decode($_POST['id']);

print_r($id);


$query = "DELETE FROM arquivo WHERE idarquivo IN $id";
$resultado = mysqli_query($con, $query);

?>

  • did not work, updated the post so you see my html if there is something wrong.

  • I updated my answer . was missing a key in this line $id = json_decode($_POST['id']);. In a little while I look, I’m tight today rs

  • I appreciate the help, but your solution didn’t work.

Browser other questions tagged

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