Passing an array with ajax to php

-1

I'm trying to do a delete of multiple records, where I try to get the input with Ajax and go to PHP .

In the console I can see that I'm getting the values, but when it comes to passing php, I can not seem to get it. I've printed in PHP and it does not even come in:

if (isset($_POST['deleting']) AND $_SERVER['REQUEST_METHOD'] == 'POST')

In Ajax the code looks like this:

$('.delete-form').submit(function(){
    var obj = this;     
    var form = $(obj);
    var submit_btn = $('.delete-form :submit');
    var submit_btn_text = submit_btn.val();
    checkbox = new Array();
        $("input[type=checkbox][name='excluir[]']:checked").each(function(){
            checkbox.push($(this).val());
        });
    console.log(checkbox)
    $.ajax({
        type: "POST",
        data: {
            id: { excluir : checkbox }
        },
        url: form.attr('action'),
        success: function() {
            alert("Houve algum erro ao excluir!");
        },
        success: function( data ) { 
                    alert(data);
            },
        error: function(){
            alert("Houve algum erro ao tentar excluir!");
        }
    });

return false;})

And in PHP it looks like this:

echo 'antes do if';
if (isset($_POST['deleting']) AND $_SERVER['REQUEST_METHOD'] == 'POST'){
    echo 'depois do if';
    $data = $_POST['excluir'];
    print_r($data);
    foreach($data as $valor){
        $sql = $pdo->prepare("delete FROM client WHERE id = '$valor'");
        $delete = $sql->execute();
        if($delete){
            echo 'OK';
        }
    }
}

Coming from Ajax , it does not even enter the if of PHP . If I use PHP without Ajax , I can delete the records.

    
asked by anonymous 23.07.2017 / 07:43

1 answer

0

Note in your PHP script, you are checking if the deleting parameter exists but did not find it in your Javascript .

Add it.

data: {
    deleting: true,
    id: { excluir : checkbox }
}
    
23.07.2017 / 08:06