v2.5.2
Giriş yap

Ajax hata mesajı hk

emmir2
328 defa görüntülendi

Merhaba kullanıcı profilinde kontrollü işlemler yaptırıyorum ajax tarafında hata mesajlarını yazdıramadım yardımlarınızı bekliyorum

if (!isset($_POST['csrf_token'])) {
    $token = [
        'token' => false
    ];
} else if ($_POST['csrf_token'] != $_SESSION['csrf']) {
    $token = [
        'token' => false
    ];
    echo json_encode($token);
} else if($_POST) {
    $query = $db->prepare("SELECT * FROM kullanici where id=:id LIMIT 1");
    $kullanici = $query->execute(array(":id" => $_SESSION['kullanici']['id']));
    $kullanici = $query->fetch(PDO::FETCH_ASSOC);

    if ($kullanici['ad'] != post('ad') || $kullanici['soyad'] != post('soyad') || $kullanici['tc'] != post('tc') || $kullanici['dogum_tarihi'] != post('dogum_tarihi')){
        $islem = $db->prepare("UPDATE kullanici SET ad = ?, soyad = ?, tc = ?, dogum_tarihi = ?, tc_verify = ? WHERE id = ?");
        $islem = $islem->execute(array(post('ad'), post('soyad'), post('tc'), post('dogum_tarihi'), 0, $_SESSION['kullanici']['id']));
    }
        $query = $db->prepare("SELECT * FROM kullanici where email=:email  AND id !=:id  LIMIT 1");
        $econt = $query->execute(array(":email" => post('email'),":id"=>$_SESSION['kullanici']['id']));
        $econt = $query->fetch(PDO::FETCH_ASSOC);
            if ($econt){
                $email = [
                    'email' => false
                ];
            }else{
                $email = [
                    'email' => true
                ];
                $islem = $db->prepare("UPDATE kullanici SET email = ?, email_verify = ? WHERE id = ?");
                $islem = $islem->execute(array(post('email'), 0, $_SESSION['kullanici']['id']));
            }
        echo json_encode($email);
        $query = $db->prepare("SELECT * FROM kullanici where telefon=:telefon AND id !=:id  LIMIT 1");
        $tcontrol = $query->execute(array(":telefon" => post('telefon'), ":id"=> $_SESSION['kullanici']['id']));
        $tcontrol = $query->fetch(PDO::FETCH_ASSOC);
            if ($tcontrol){
                $phone = [
                    'phone' => false
                ];
            }else{
                $phone = [
                    'phone' => true
                ];
                $islem = $db->prepare("UPDATE kullanici SET telefon = ? WHERE id = ?");
                $islem = $islem->execute(array(post('telefon'),$_SESSION['kullanici']['id']));
            }
        $query = $db->prepare("SELECT * FROM kullanici where sef=:sef  AND id !=:id  LIMIT 1");
        $scont = $query->execute(array(":sef" => post('sef'),":id"=>$_SESSION['kullanici']['id']));
        $scont = $query->fetch(PDO::FETCH_ASSOC);
        if ($scont){
            $username = [
                'username' => false
            ];
        }else{
            $username = [
                'username' => true
            ];
            $islem = $db->prepare("UPDATE kullanici SET sef = ? WHERE id = ?");
            $islem = $islem->execute(array(post('sef'),$_SESSION['kullanici']['id']));
        }
        echo json_encode($username);

        $islem = $db->prepare("UPDATE kullanici SET  sifre = ?,iban = ?, cinsiyet = ? WHERE id = ?");
        $islem = $islem->execute(array(post('sifre'), post('iban'), post('cinsiyet'), $_SESSION['kullanici']['id']));
        if ($islem) {
            $response = [
                'status' => true
            ];
            $dogum = date('Y', strtotime(post('dogum_tarihi')));
            $bilgiler = array(
                "isim" => post('ad'),
                "soyisim" => post('soyad'),
                "dogumyili" => $dogum,
                "tcno" => post('tc')
            );
            $sonuc = tcno_dogrula($bilgiler);
            if ($sonuc == "true") {
                $islem = $db->prepare("UPDATE kullanici SET tc = ?, tc_verify = ? WHERE id = ?");
                $islem = $islem->execute(array(post('tc'), 1, $_SESSION['kullanici']['id']));
                $response = [
                    'status' => true
                ];
            } else {
                $islem = $db->prepare("UPDATE kullanici SET  tc = ?, tc_verify = ? WHERE id = ?");
                $islem = $islem->execute(array(post('tc'), 0, $_SESSION['kullanici']['id']));
                $response = [
                    'status' => false
                ];
            }
        } else {
            $response = [
                'status' => false
            ];
        }

    echo json_encode($response);
}else{
    header('Location:anasayfa');
}

<script>
    $(function () {
    $("#user_update").on("submit", function (e) {
        $(".setting__button").html("<i class='fa fa-spinner fa-spin'></i>");
        e.preventDefault()
        $.ajax('api/user-update.php', {
            method: 'POST',
            data: $(this).serialize(),
            dataType: "json",
        }).then(success => {
            if (success.status) {
                setTimeout(function () {
                    $('#log').html('İşlem Başarılı');
                }, 2000);
                setTimeout(() => {
                    $(".setting__button").html("Güncelle");
                }, 2000);
            } else {
                setTimeout(function () {
                    $('#log').html('Bir hata oluştu');
                }, 5000);
                setTimeout(() => {
                    $(".setting__button").html("Güncelle");
                }, 5000);
            }
            if (success.token) {
            } else {
                $('#tokenlog').html('Token ile oynama');
            }
            if (success.email) {
                $('#emaillog').html('Email Değişikliği Başarılı');
            } else {
                setTimeout(function () {
                        $('#emaillog').html('Email Kullanımda');
                    },2000);
            }
            if (success.username) {
                $('#seflog').html('Kullanıcı Adı  Değişikliği Başarılı');
            } else {
                setTimeout(function () {
                    $('#seflog').html('Kullanıcı Adı Kullanımda');
                },2000);
            }
            if (success.phone) {
                $('#phonelog').html('Telefon Numarası Değişikliği Başarılı');
            } else {
                setTimeout(function () {
                    $('#phonelog').html('Telefon Numarası Kullanımda');
                },2000);
            }
        }, fail => {
            console.log(`Server hatası: ${fail}`)
        })
    })
})
</script>



codepacks
498 gün önce

AJAX tarafında hata mesajlarını göstermek için, ilk olarak AJAX isteğinizi gönderirken error callback fonksiyonunu tanımlamanız gerekiyor.
Bu fonksiyon, AJAX isteği başarısız olduğunda çalıştırılacak ve hata mesajlarını göstermek için kullanılacak.
Örnek olarak, aşağıdaki kod bloğu AJAX isteği başarısız olduğunda hata mesajı gösterebilir:

$.ajax({
  url: '/update_profile',
  type: 'POST',
  data: { ... },
  success: function(response) {
    // İşlem başarılı olduğunda yapılacak işlemler
  },
  error: function(xhr, status, error) {
    // İşlem başarısız olduğunda yapılacak işlemler
    alert('Hata: ' + error);
  }
}); 

Bu örnekte, AJAX isteği başarısız olduğunda error callback fonksiyonu çalıştırılır ve hata mesajı alert ile gösterilir.
Bu örnekteki error değişkeni, hata mesajının tam metnini içerir. Hata mesajını göstermek için bu değişkeni kullanabilirsiniz.

Not: Bu örnek jQuery kütüphanesi kullanılarak verilmiştir.
Eğer farklı bir JavaScript kütüphanesi veya vanilla JavaScript kullanıyorsanız, yukarıdaki örnekteki kod bloğunu uyarlamanız gerekebilir.