Error converting Array in Ajax to Array in PHP

0

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

?>
    
asked by anonymous 09.03.2017 / 16:22

2 answers

1

The best way would be to make a jSON and move to data: {id:id} so in PHP it was only to decode and loop in the delete command. But a possible solution is the below. It concatenates each ID and in PHP it makes each ID stay as 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);

}
    
09.03.2017 / 21:06
0

Try using POST instead of REQUEST. See if it works! In the ajax request change the method:'POST' to type: 'POST' and make your php so 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);

? >

    
09.03.2017 / 17:02