1
$('#btnmails').on('click', function(){
    let ids = $.map($('.atitle'), (e) => $(e).attr('data-id'));
    ids = JSON.stringify(ids);
    console.log(ids);  // ["26","25","24","23","22","21"]
    $.post('pro.php', {fn: 'btn_mails', args: [ids]}, function(data){
        console.log(data);  // 0 - but it is not true
    });
});

pro.php

function btn_mails($ids){
    global $db;
    $sq = "select distinct mail from clients where id in ('" . $ids . "')";
    $st = $db->prepare($sq);
    $st->execute();
    $arr = $st->fetchAll();
    echo count($arr);
}

I have all six ids in table and each of them with a distinct mail
so result should be 6 and not 0
pls help

provance
  • 877
  • 6
  • 10

1 Answers1

1

I think that your problem is in your php function

Probably $ids is an array so you have to transform it in a string using implode

like this

function btn_mails($ids){
    global $db;
    $sq = "select distinct mail from clients where id in (" . implode(', ', $ids) . ")";
    $st = $db->prepare($sq);
    $st->execute();
    $arr = $st->fetchAll();
    echo count($arr);
}

R4ncid
  • 6,944
  • 1
  • 4
  • 18
  • `$str = implode(', ', $ids);` and `$sq = "select distinct mail from prijave where id in (" . $str . ")";` works - without `stringify` on client side. Thanks a lot – provance May 17 '22 at 08:21