简体   繁体   中英

How to upload image without using submit button?

I am doing image upload without using submit button. When user choose a image, he upload immediately. But I have a problem, Ajax only print "succes", but image is not in folder. PHP without ajax work.

This is my html:

<form action="../PHP/fotogaleria.php" method="post" class="form_fotogaleria" enctype="multipart/form-data">
    <div class="obal_fotogalerie">
        <div class="stvorcek">
            <label class="file_nahod">
                <input type="file" name="odoslat_fotogaleria" class="odid">
                <img class="priecinok" src="../Obrazky/folder.png">
                <p> Nahrať</p>
            </label>
        </div> <!-- koniec "stvorcek" -->
    </div> <!-- koniec "obal_fotogalerie" -->
</form>

PHP:

$name = $_FILES['odoslat_fotogaleria']['name'];
$tmp_name = $_FILES['odoslat_fotogaleria']['tmp_name'];
$path = '../Obrazky-zvieratok/';
$cielovy_file = $path . basename($name);


move_uploaded_file($tmp_name,$path.$name);
echo "success";

And here is AJAX:

$('.odid').change(function(e) {
    e.preventDefault();
$.ajax({
 url: '../PHP/fotogaleria.php',
 type: "POST",
 data: new FormData(this),
 contentType: false,
 cache: false,
 processData: false,
 success: function(data){
    console.log(data);
 }

Can someone help?

You're passing the input element to the FormData constructor. You should pass the form element instead.

data: new FormData($(this).closest('form').get(0)),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM