Checked value post with ajax and php?

Deleting data with multiple selections did not work.
one by one I can delete it with a similar code.
but I want to delete multiple records with the checkbox.

index.html

<input type="checkbox" id="checkbox1-1" value="<?php echo $kategori_id; ?>" name="coklu[]">             
            
    <button id="btn_del" type="submit" class="btn btn-md btn-danger" data-toggle="tooltip" title="Seçili olanları sil" data-toggle="tooltip" data-placement="right"><i class="fa fa-trash-o"></i> Delete</button>

script code

<script>
        $(document).ready(function(){
            readProducts(); 
            $(document).on('click', '#btn_del', function(e){
                var data = $("#checkbox1-1").serializeArray();
                data.is(":checked")
                SwalDeletes(data.val());
                e.preventDefault();
            });
        });

        function SwalDeletes(data){
            swal({
                title: 'Emin misiniz?',
                text: "Seçilen kategoriler kalıcı olarak silinecek!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Evet',
                cancelButtonText: 'İptal',
                showLoaderOnConfirm: true,

                preConfirms: function() {
                    return new Promise(function(resolve) {
                    $.ajax({
                            url: 'delete.php',
                            type: 'POST',
                            data: data,
                            dataType: 'json'
                        })
                        .done(function(response){
                            swal(responses.titles, responses.message, responses.status);
                            //readProducts();
                        })

                        .fail(function(){
                            swal(responses.titles, responses.message, responses.status);
                            //readProducts();
                        });
                    });
                },
                allowOutsideClick: false              
            });
        }

        function readProducts(){
            $('#load-products').load('index.php');   
        }

    </script>

delete.php

<?php
    include_once 'connect.php';

    header('Content-type: application/json; charset=UTF-8');
    $responses = array();


    $secilenler = implode($_POST['data'],', ');
    $result = mysqli_query($con, "SELECT * FROM tb_haber_kategori WHERE kategori_id IN ($secilenler)");

    while ($row=mysqli_fetch_assoc($result)) {
        
        $coklu_sonuc = mysqli_query($con,"DELETE FROM tb_haber_kategori WHERE kategori_id IN ($secilenler)");

        if($coklu_sonuc>0){
            $responses['status']  = 'success';
            $responses['titles']  = 'SİLİNDİ';
            $responses['message'] = 'Kategori silme başarılı ...';
        }
        else {
            $responses['status']  = 'error';
            $responses['titles']  = 'HATA';
            $responses['message'] = 'Bir sorun oluştu ...
            <br>Lütfen bu durumu ProPANEL yetkililerine bildirin..!';
        }
    }

    echo json_encode($responses);

Where is my mistake?
can you help me?

You treat an array as a jQuery object (data.is(":checked")), but the error console would have told you that earlier.

Despite that, on the JS-side of things, you make data collection too complicated. It suffices to use serialize() on the checkboxes (no additional checks needed).

In PHP you have a gaping SQL injection hole. The select before the delete is completely unnecessary and you’re missing the json header for the response.

2 Likes

Could you code me how to do it?

No .

Why don’t you try yourself first?

I’m trying, my ajax and script experience is bad :frowning:

Well, for me at least the best way to learn is to have a go. If someone presents you with a solution without you doing anything, you won’t gain any experience. That might sound daunting and unhelpful, but it’s got to be worth you having a go?

I think you are right, thank you for your interest :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.