通过单击“提交”按钮访问通过FormData传递的数据

时间:2015-12-15 05:50:12

标签: php jquery ajax forms validation

我有一个包含多个输入字段的表单,包括文件输入。我正在使用jquery validate plugin.I需要使用ajax提交表单。

HTML:

<form method="post" name="addstudent" id="registrationform" enctype="multipart/form-data">
some fields
<input type="submit" id="buttontext" 
    class="student_registrationform_button" value="submit" />

Jquery的:

$('#buttontext').click(function(){
    $("#registrationform").validate({
           invalidHandler: function(event, validator) {    
          //some code        
    },
 submitHandler: function(form){
            formdata = false;
            if (window.FormData) {
                    formdata = new FormData();
                }
           var i = 0, len = $(".file").length, img, reader, file;
           $('.file').each(function() { var file = this.files[0];
            if (window.FileReader) {
                            reader = new FileReader();
                            reader.readAsDataURL(file);
                     }
                if (formdata) {
                    formdata.append("file", file);
                }
           });
          $.ajax({
                    url:  "process.php",
                    type: 'POST',
                    //data: $(this).serialize(),
                    data: formdata,
                    cache: false,
                    contentType: false,
                    processData: false, 
                    success:function(data){  //alert(data);
                    console.log(data);return false;
          }

Process.php

当我检查echo 'action:'.var_dump($_POST);时,它显示空字符串

2 个答案:

答案 0 :(得分:2)

    (function($) {
    $.fn.serializefiles = function() {
        var obj = $(this);

        var form_data = new FormData(this[0]);
        $.each($(obj).find('input[type="file[]"]'), function(i, tag) {
            $.each($(tag)[0].files, function(i, file) {
                form_data.append(tag.name, file);
            });
        });

        var params = $(obj).serializeArray();
        $.each(params, function (i, val) {
            form_data.append(val.name, val.value);
        });

        return form_data;
    };
    })(jQuery);


    var form_id = $('#registrationform');


    $.ajax({
        type: "POST",
        url: "process.php",
        data: form_id.serializefiles(),
        cache: false,
        processData: false,
        contentType: false,
        success: function (response) {
            console.log(data);return false;
        }


    });

答案 1 :(得分:1)

尝试这个简单的功能:

 submitHandler: function(form){
          formdata = new FormData(form);
          $.ajax({
                    url:  "process.php",
                    type: 'POST',
                    //data: $(this).serialize(),
                    data: formdata,
                    cache: false,
                    contentType: false,
                    processData: false, 
                    success:function(data){  //alert(data);
                    console.log(data);return false;
          }