简体   繁体   English

使用Ajax在单个表单上上传文件和表单数据

[英]file upload and form data on single form using ajax

I have a html form with 2 input filed and a file upload form and i want to send these data to some index.php file using ajax what i did so far is 我有一个带有2个输入文件的HTML表单和一个文件上传表单,我想使用ajax将这些数据发送到一些index.php文件,到目前为止我所做的是

<form class="col-md-3" id="form" name="reg" method="post" action="index.php" enctype="multipart/form-data">
  <label>Name</label>
  <input type="text" name="name" class="form-control" id="name">
  <label>Address</label>
  <input type="text" name="address" class="form-control">
  <div id="sugg">

  </div>
  <input type="file" name="file">
  <input type="button" name="submit" class="btn-default" id="btn" value="submit">
</form>

jquery for send data using ajax is 使用ajax发送数据的jQuery是

$("#btn").click(function() {
  $.ajax({
    url: "index.php",
    type: "POST",
    data: new FormData($('form')[0]),
    cache: false,
    conentType: false,
    processData: false,
    success: function(result) {
      console.log(result);
    }
  });

});

php file has just this php文件就是这样

echo $_POST["name"];

but in my browser window i am undefined index 但是在我的浏览器窗口中,我是未定义的索引

I found similar questions but all that doesn't solved my problem that's why i asking your help please help me to find my error 我发现了类似的问题,但所有问题仍未能解决,这就是为什么我向您求助,请帮助我找到错误

I am create below code according to my need and you can make changes according to your requirement: 我根据需要创建以下代码,您可以根据需要进行更改:

when you click upload/select file button use this function : 当您单击上传/选择文件按钮时,请使用以下功能:

$('input[type=file]').on('change', function(e){
        e.preventDefault();
        $this = $(this);
        if($this.prop("files")){
            if(setImagestoUpload(e, $this.prop("files"), $this.attr("name"))){
                var reader = new FileReader();
                reader.onload = function(e){
                    $('.brandLogo img').attr("src", e.target.result);

                    $this.parent().prev("img.imageSelection").attr("src", e.target.result);

                };
                reader.readAsDataURL(this.files[0]);
            }
        }
    });
function setImagestoUpload(e, $file, name){
        e.preventDefault();
        var type = [];
        var isSize = [];

        $($file).each(function(i, v){
            type[i] = isImage(v);
            isSize[i] = isValidSize(2, v.size);
        });
        if($.inArray(false, type) === -1){
            if($.inArray(false, isSize) === -1){
                /*if(checkTotalFileInputs() > 1 && files.length>0){
                    $.each($file, function(ind, val){
                        files[name] = val;
                    });
                    files[name] = $file[0];
                }else{
                    files = $file;
                }*/
                formData.append(name, $file[0]);
                console.log(files);
                return true;
            }else{
                alert("Error: Upload max size limit 2MB");
            }
        }else{
            alert("Please select only image file format to upload !");
            return false;
        }
    }
function isImage(file){
    var type = file.type.split("/");
    if(type[0] === "image"){
        return true;
    }else{
        return false;
    }
}

and when you submit form then use below function: 当您提交表单时,请使用以下功能:

function fileAndFormSubmit(ev, $this, get, options){

        var defaults = {
            redirect : false,
            redirectTo : "#",
            redirectAfter : 5000
        };
        var settings = $.extend({}, defaults, options);
        removeErrorAndSuccess();
        var $form = $($this).parent('form');
        /*var formData = new FormData();
        if(files.length > 0){
            $.each(files, function(key, val){
                formData.append(key, val);
            });
        }*/
        var doc = {
            data : $form.serialize()
        };

        formData.append("doc", doc.data);

        /*Call to ajax here*/
    }

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

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