v2.5.2
Giriş yap

Ajax hata mesajı hk

emmir2
327 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>



abdullahx
497 gün önce
if($_POST) {
    if (!isset($_POST['csrf_token'])) {
        $response = [
            'status' => false,
            'text' => 'Token ile oynama!'
        ];
    }
    else if ($_POST['csrf_token'] != $_SESSION['csrf']) {
        $response = [
            'status' => false,
            'text' => 'Token ile oynama!'
        ];
    }
    else {
        $query = $db->query("SELECT * FROM kullanici WHERE 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([
                post('ad'),
                post('soyad'),
                post('tc'),
                post('dogum_tarihi'),
                0,
                $_SESSION['kullanici']['id']
            ]);
        }
        $successText = [];
        $errorText = '';
        $query = $db->prepare("SELECT id, email, telefon, sef FROM kullanici WHERE (email = ? OR telefon = ? OR sef = ?)  AND id != ?");
        $query->execute([
            post('email'),
            post('telefon'),
            post('sef'),
            $_SESSION['kullanici']['id']
        ]);
        if ($query->rowCount()) {
            $control = $query->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE);
            foreach ($control as $item) {
                if ($item['email'] == post('email')) {
                    $response = [
                        'status' => false,
                        'text' => 'Email Kullanımda'
                    ];
                    goto end;
                }
                if ($item['telefon'] == post('telefon')) {
                    $response = [
                        'status' => false,
                        'text' => 'Telefon Numarası Kullanımda'
                    ];
                    goto end;
                }
                if ($item['sef'] == post('sef')) {
                    $response = [
                        'status' => false,
                        'text' => 'Kullanıcı Adı Kullanımda'
                    ];
                    goto end;
                }
            }
        }

        $dogum = date('Y', strtotime(post('dogum_tarihi')));
        $bilgiler = [
            "isim" => post('ad'),
            "soyisim" => post('soyad'),
            "dogumyili" => $dogum,
            "tcno" => post('tc')
        ];
        $sonuc = tcno_dogrula($bilgiler) == 'true';

        if ($sonuc) {
            $islem = $db->prepare("UPDATE kullanici SET ad = ?, soyad = ?, tc = ?, tc_verify = ?, dogum_tarihi = ?, email = ?, email_verify = ?, telefon = ?, sef = ?, sifre = ?, iban = ?, cinsiyet = ? WHERE id = ?");
            $islem->execute([
                post('ad'),
                post('soyad'),
                post('tc'),
                1,
                post('dogum_tarihi'),
                post('email'),
                0,
                post('telefon'),
                post('sef'),
                post('sifre'),
                post('iban'),
                post('cinsiyet'),
                $_SESSION['kullanici']['id']
            ]);
            if ($islem->rowCount()) {
                $response = [
                    'status' => true,
                    'text' => 'İşlem başarılı!'
                ];
            }
            else {
                $response = [
                    'status' => false,
                    'text' => 'İşlem tamamlanamadı!'
                ];
            }
        }
        else {
            $response = [
                'status' => false,
                'text' => 'Kimlik bilgileriniz doğrulanamadı!'
            ];
        }
    }
    end:
    echo json_encode($response);
}
else {
    header('Location:anasayfa');
}
$(function () {
        $("#user_update").on("submit", function (e) {
            e.preventDefault()
            let setBtn = $(".setting__button")
            $.ajax('api/user-update.php', {
                method: 'POST',
                data: $(this).serialize(),
                dataType: "json",
                beforeSend() {
                    setBtn.html("<i class='fa fa-spinner fa-spin'></i>")
                }
            }).then(success => {
                if (success.status)
                    $('#log').html(success.text).addClass('text-success').removeClass('text-danger');
                 else
                    $('#log').html(success.text).addClass('text-danger').removeClass('text-success');
            }, fail => {
                console.log(`Server hatası: ${fail}`)
            }).always(() => {
                setBtn.html("Güncelle");
            })
        })
    })