v2.5.2
Giriş yap

Jquery Ajax ile PHP veritabanı veri güncelleme

aorkuneren
100 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Ajax ile Tayfun Hoca'nın Udemy'deki eğitim setindeki klasör yapısına göre, checkbox'a tıklanarak Ajax ile veritabanı güncelleme işlemi yapmak istiyorum. Ancak bir türlü istediğim sonucu alamadım.

Herhangi bir hata mesajı yok. Checkbox'a tıkladığımda console'da veriler görünüyor. Fakat sayfada ya da veritabanında herhangi bir değişiklik olmuyor.

Yardımcı olabilir misiniz?
Console Çıktısı

{id: '1', status: 0, action: 'product_status'}

admin_view('index') > HTML

<div class="product-status">
    <label>
        <div>
            <input type="checkbox" name="status" id="<?= $product['id']?>" role="switch" <?= $product['status'] == "1" ? "checked" : null;?>>
        </div>
   </label>
</div>

admin_view('index') > Jquery

$('.product-status input[type="checkbox"]').change(function() {
        var product_status = $(this).is(':checked') ? 1 : 0;
        var product_id = $(this).attr('id');

        var data = {
            id: product_id,
            status: product_status,
            action: 'product_status'
        };

        console.log(data); // Verileri konsola yazdır

        $.ajax({
            type: 'POST',
            url: window.location.href + '?action=product_status',
            data: data,
        });
    });

admin_controller('index') > PHP

if (post('action') == 'product_status') {
    $product_id = post('id');
    $product_status = post('status');

    $update = $db->update('products')
        ->where('id', $product_id)
        ->set([
            'status' => $product_status
        ]);

    if ($update){
        echo "İşlem Başarılı"
        echo json_encode(['status' => 'success']);
        exit;
    } else {
        echo "İşlem Tamamlanamadı.<br>";
        echo json_encode(['status' => 'error']);
    }
    exit;
}
Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!