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.