• 06-06-2023, 23:14:39
    #1
    Merhaba arkadaşlar, bu konuyu jquery mi ajax mı nodejs bölümündemi açmam gerekiyordu bilmiyorum.


    Boş bir Php sayfam var, bu sayfaya 1 adet checkbox koysam. Bu checkboxta mysql deki bir veriye bağlı. Php sayfasını yenilemeden mysqldeki veriye göre canlı bir şekilde checkbox değişsin istiyorum. Yardımcı olabirmisiniz?
  • 06-06-2023, 23:28:49
    #2
        $(document).ready(function() {
            setInterval(function() {
                $.ajax({
                    url: 'veritabani_kontrol.php', 
                    method: 'GET',
                    success: function(response) {
                        if (response == 1) {
                            $('#checkbox').prop('checked', true)
                        } else {
                            $('#checkbox').prop('checked', false);
                        }
                    }
                });
            }, 1000);
        });
  • 06-06-2023, 23:38:34
    #3
    index.html

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <input type="checkbox" id="checkbox">
    
    <script>
     setInterval(function() {
     
        $.ajax({
            url: 'kontrol.php',
            type: 'POST',
            dataType: 'json',
            success: function(response) {
                if (response.veri === '1') {
                    $('#checkbox').prop('checked', true);
                } else {
                    $('#checkbox').prop('checked', false);
                }
            }
        });
     }, 2000); // 2 saniyede bir kontrol edilecek...
    
    </script>
    kontrol.php

    <?php
    // Mysql bağlantısı yapılacak
    $host             = "localhost";
    $db_kullanici   = "kullanici_adi";
    $db_sifre        = "sifre";
    $db_ismi        = "veritabani_adi";
    
    // Mysql bağlantısını oluştur
    $db = new mysqli($host, $db_kullanici, $db_sifre, $db_ismi);
    
     
    $tablo_adi = "veriler"; // hangi tablodsaysa onu yazın
    $alan        = "alan" // tablodaki kontrol edilecek alan
    
    if ($db->connect_error) {
        die("Bağlantı hatası: " . $db->connect_error);
    }
    
    $sql = "SELECT ".$alan." FROM ".$tablo_adi." WHERE id = 1";
    $sonuc = $db->query($sql);
    
    if ($sonuc->num_rows > 0) {
        $cevap = $sonuc->fetch_assoc();
        $veri = $cevap[$alan];
    } else {
        $veri = '';
    }
    
    echo json_encode(array($alan => $veri));
    $db->close();
    ?>
  • 07-06-2023, 22:41:32
    #4
    F12 adlı üyeden alıntı: mesajı görüntüle
    index.html

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <input type="checkbox" id="checkbox">
    
    <script>
     setInterval(function() {
     
        $.ajax({
            url: 'kontrol.php',
            type: 'POST',
            dataType: 'json',
            success: function(response) {
                if (response.veri === '1') {
                    $('#checkbox').prop('checked', true);
                } else {
                    $('#checkbox').prop('checked', false);
                }
            }
        });
     }, 2000); // 2 saniyede bir kontrol edilecek...
    
    </script>
    kontrol.php

    <?php
    // Mysql bağlantısı yapılacak
    $host             = "localhost";
    $db_kullanici   = "kullanici_adi";
    $db_sifre        = "sifre";
    $db_ismi        = "veritabani_adi";
    
    // Mysql bağlantısını oluştur
    $db = new mysqli($host, $db_kullanici, $db_sifre, $db_ismi);
    
     
    $tablo_adi = "veriler"; // hangi tablodsaysa onu yazın
    $alan        = "alan" // tablodaki kontrol edilecek alan
    
    if ($db->connect_error) {
        die("Bağlantı hatası: " . $db->connect_error);
    }
    
    $sql = "SELECT ".$alan." FROM ".$tablo_adi." WHERE id = 1";
    $sonuc = $db->query($sql);
    
    if ($sonuc->num_rows > 0) {
        $cevap = $sonuc->fetch_assoc();
        $veri = $cevap[$alan];
    } else {
        $veri = '';
    }
    
    echo json_encode(array($alan => $veri));
    $db->close();
    ?>
    Evet, çalıştı. Teşekkür ederim.