简体   繁体   English

显示甜蜜警报后如何在php中使用ajax上传文件?

[英]how to upload a file using ajax in php after showing sweet alert?

Im currently having a trouble to insert an file input (images) into my database, but it return me empty value when I'm trying to insert it into database. 我目前在将文件输入(图像)插入数据库时​​遇到麻烦,但是当我尝试将其输入数据库时​​会返回空值。

this is my script 这是我的剧本

<script>
        $("#insertform").on('submit',(function(e) {
/*          $('#submit').toggleClass('active');
            document.getElementById('submit').disabled = 'disabled'; */
            var data=$('#insertform').serialize();
            e.preventDefault();
            swal({   
                title: "Are you sure",   
                text: "Do you want to save hotel data",   
                type: "info",   
                showCancelButton: true,   
                closeOnConfirm: false,   
                showLoaderOnConfirm: true, }, 
                function(){   
                    setTimeout(function(){      
                    $.ajax({
                        url: "hotels/insert_Hotel.php",
                        type: "POST",
                        data: new FormData(this),
                        async: false,
                        success: function(data){
                            swal("Saved! your hotel data has been saved");
                            location.reload();
                        },
                        contentType: false,
                        cache: false,
                        processData:false, 
                        error: function(){
                            alert('error handing here');
                        }
                    });    
                    }, 2000); 
                }); 
        }));
    </script>

this is my php file 这是我的PHP文件

<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['logo']['tmp_name'])) {
$randomvalue=rand(1,999999999);
$date=date("Y-m-d H:i:s a");
$sourcePath = $_FILES['logo']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']."/hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
$dbpath="../../hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
move_uploaded_file($sourcePath,$targetPath);
}
else{
    echo 'file not uploaded';
}
}
?>

it doesn't upload the file in to the folder and doesn't insert values to the table... 它不会将文件上传到文件夹中,也不会在表中插入值...

new FormData(this)在这里this不是形式,而是你需要传递的形式,你的new FormData(form)在阿贾克斯:

data: new FormData($('#insertform')[0]), // <---change to this.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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