v2.5.2
Giriş yap

Jquery post ile file verilerini ajax ile kaydetme

edward
368 defa görüntülendi

Merhaba arkadaşlar
Jquery'in post metodu ile verilerimi veritabanına kayıt edebiliyorum fakat input değerini file yapınca kayıt edemiyorum seçmiş olduğum dosyayı. Sorunu sadece jquery post 'tan kaynaklı PHP kısmında bir sorun yok.
Bu konuda yardımcı olur musunuz kullanmış olduğum kod aşağıdaki gibidir.

     function preregistration(form) {
		     var data = $(form).serialize();  
			$.post('/ajax/?do=preregistration', data, function (result) {
				if (result.error) {
					Snackbar.show({
						text: result.error,
						actionText: "Tamam",
						actionTextColor: "#fff",
						pos: 'bottom-left'
					});
				} else {
					Snackbar.show({
						text: result.success,
						actionText: "Tamam",
						actionTextColor: "#fff",
						pos: 'bottom-left'
					});
				}
			}, 'json');
		} 
abdullahx
825 gün önce

index

<!doctype html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<form action="" id="form">
    <input type="text" name="name" placeholder="Ad"><br>
    <input type="text" name="surname" placeholder="Soyad"><br>
    <input type="password" name="password" placeholder="Şifre"><br>
    <input type="file" name="resim"><br><br>
    <input type="file" name="resim3"><br><br>
    <input type="submit">
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $(function () {
        [...document.querySelectorAll("form")].forEach(e => {
            e.addEventListener("submit", function (f) {
                f.preventDefault()
                formSubmit(this)
            })
        })
    })

    function formSubmit(form) {
        let data = new FormData();
        $(form).find("input[type='file']").each(function (index, element) {
            if(element.files.length)
                data.append($(this).attr("name"), element.files[0])
        })
        data.append('formData', $(form).serialize())
        $.ajax('ajax.php', {
            method: "POST",
            processData: false,
            contentType: false,
            data: data,
        }).then(response => {
            console.log(response)
        })
    }

</script>
</body>
</html>

ajax.php

<?php

print_r($_FILES);

print_r($_POST);

Çıktı

Array
(
    [resim] => Array
        (
            [name] => items.sql
            [type] => application/octet-stream
            [tmp_name] => C:\xampp\tmp\php59FE.tmp
            [error] => 0
            [size] => 3294
        )

    [resim3] => Array
        (
            [name] => bolum7.pdf
            [type] => application/pdf
            [tmp_name] => C:\xampp\tmp\php5A0F.tmp
            [error] => 0
            [size] => 3648448
        )

)
Array
(
    [formData] => name=abdullah&surname=kaya&password=1234567
)