使用jquery.filer拖放提交其他表单数据

时间:2016-06-27 20:13:19

标签: ajax forms multipartform-data oscommerce

首先,我对编程非常陌生,所以请耐心等待。

我创建了一个包含多个字段的表单。我想使用拖放文件上传字段,但也提交其余的表单数据。

我使用jQuery.filer作为拖放脚本。

问题在于我无法弄清楚如何在文件上传时发布其余数据。

非常感谢任何帮助/指导。 以下是我用于操作的文件。这内置于oscommerce后端,这就是为什么你会在upload.php文件中看到tep_db_query函数。

HTML

      <script> 
         $(function() {
         $( "#email-box" ).draggable(); 
          });

          </script>        
        <div id="email-box" class="email-popup">
        <a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
    <table width="650" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="3" align="center"><strong>Upload New Email</strong></td> 
      </tr>
    </table>

<table width="650" border="0" cellspacing="15" cellpadding="10" align="center">  
  <form action="upload.php" id="EmailFormSubmit" name="EmailFormSubmit" enctype="multipart/form-data" method="post">
  <tr>
    <td width="100%" valign="top">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
 <div class="form_field_style wire_balance"><strong>Date Of Email</strong><br />&nbsp;&nbsp;&nbsp;<input name="email_date" id="email_date" class="input_field_long" size="24" /></div>
 <div class="form_field_style wire_balance"><strong>Contact Name</strong><br />&nbsp;&nbsp;&nbsp;<input name="email_name" id="email_name" class="input_field_long" size="24" /></div>
  <div class="form_field_style wire_balance"><strong>Email Address</strong><br />&nbsp;&nbsp;&nbsp;<input name="email_address" id="email_address" class="input_field_long" size="24" /></div>

 <div class="form_field_style wire_balance"><strong>Upload The Email</strong><br /><br /><input type="file" name="files[]" id="filer_input2"></div>   


</td>
  </tr>
</table> 


    </td>
  </tr>

  <tr>
    <td align="center">
     <input name="order_id" id="order_id" value="<?php echo $oID ?>" class="input_field" size="24" type="hidden">

    </td> 
  </tr>
</form>
</table>
        </div>

        <div>
            <div class="btn-sign">
                <a href="#email-box" class="email-window"><span class="button_text_color">Add New Document</span></a>
            </div>
        </div>

<script>
$("#email_date").datetimepicker({
    dateFormat:'yy-mm-dd',
    controlType: 'select',
    timeFormat: 'hh:mm tt'
});

</script>

upload.php的

    <?php

  //    echo '<pre>'; print_r($_POST);  print_r($_FILES);die;


require('includes/application_top.php'); 
include('class.uploader.php');

    $uploader = new Uploader();
    $data = $uploader->upload($_FILES['files'], array( 
        'limit' => 1, //Maximum Limit of files. {null, Number}
        'maxSize' => 500, //Maximum Size of files {null, Number(in MB's)}
        'extensions' => array('pdf','msg','docx','doc','xls','xlsx','txt','jpg','gif','png','tiff','bmp','eml'), //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
        'required' => true, //Minimum one file is required for upload {Boolean}
        'uploadDir' => 'uploads/emails/', //Upload directory {String}
        'title' => array('name'), //New file name {null, String, Array} *please read documentation in README.md
        'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
        'perms' => null, //Uploaded file permisions {null, Number}
        'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
        'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback
        'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback
        'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
        'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback
        'onRemove' => 'onFilesRemoveCallback' //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
    ));

    if($data['isComplete']){
        $files = $data['data'];
        print_r($files);
    }

    if($data['hasErrors']){
        $errors = $data['errors'];
        print_r($errors);
    }

    $curr_aircraft = filter_var($_POST["curr_aircraft"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $email_name = filter_var($_POST["email_name"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $email_address = filter_var($_POST["email_address"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $email_date = date_create($_POST["email_date"]); 
    $email_copy = filter_var($_POST["email_copy"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $escrow_person = filter_var($_POST["escrow_person"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $order_id = filter_var($_POST["order_id"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);  

//$EmailDate=date_create("2013-03-15");
$dateTimeFormated = date_format($email_date,"Y/m/d H:i:s"); 


    $insert_row = tep_db_query("INSERT INTO orders_email(date_added, email_address, email_name, email_copy, escrow_person, orders_id) VALUES ('".$dateTimeFormated."', '".$email_address."', '".$email_name."', '".$new_file_name."', '".$escrow_person."', '".$order_id."')");



    function onFilesRemoveCallback($removed_files){
        foreach($removed_files as $key=>$value){
            $file = 'uploads/emails/' . $value;
            if(file_exists($file)){
                unlink($file);
            }
        }

        return $removed_files;
    }


?>

custom.js

$(document).ready(function() {

    //Example 1
    $('#filer_input').filer({
        showThumbs: true
    });

    //Example 2
    $("#filer_input2").filer({
        limit: null,
        maxSize: null,
        extensions: null,
        changeInput: '<div class="jFiler-input-dragDrop"><div class="jFiler-input-inner"><div class="jFiler-input-icon"><i class="icon-jfi-cloud-up-o"></i></div><div class="jFiler-input-text"><h3>Drag&Drop files here</h3> <span style="display:inline-block; margin: 15px 0">or</span></div><a class="jFiler-input-choose-btn blue">Browse Files</a></div></div>',
        showThumbs: true,
        theme: "dragdropbox",
        templates: {
            box: '<ul class="jFiler-items-list jFiler-items-grid"></ul>',
            item: '<li class="jFiler-item">\
                        <div class="jFiler-item-container">\
                            <div class="jFiler-item-inner">\
                                <div class="jFiler-item-thumb">\
                                    <div class="jFiler-item-status"></div>\
                                    <div class="jFiler-item-info">\
                                        <span class="jFiler-item-title"><b title="{{fi-name}}">{{fi-name | limitTo: 25}}</b></span>\
                                        <span class="jFiler-item-others">{{fi-size2}}</span>\
                                    </div>\
                                    {{fi-image}}\
                                </div>\
                                <div class="jFiler-item-assets jFiler-row">\
                                    <ul class="list-inline pull-left">\
                                        <li>{{fi-progressBar}}</li>\
                                    </ul>\
                                    <ul class="list-inline pull-right">\
                                        <li><a class="icon-jfi-trash jFiler-item-trash-action"></a></li>\
                                    </ul>\
                                </div>\
                            </div>\
                        </div>\
                    </li>',

            itemAppend: '<li class="jFiler-item">\
                            <div class="jFiler-item-container">\
                                <div class="jFiler-item-inner">\
                                    <div class="jFiler-item-thumb">\
                                        <div class="jFiler-item-status"></div>\
                                        <div class="jFiler-item-info">\
                                            <span class="jFiler-item-title"><b title="{{fi-name}}">{{fi-name | limitTo: 25}}</b></span>\
                                            <span class="jFiler-item-others">{{fi-size2}}</span>\
                                        </div>\
                                        {{fi-image}}\
                                    </div>\
                                    <div class="jFiler-item-assets jFiler-row">\
                                        <ul class="list-inline pull-left">\
                                            <li><span class="jFiler-item-others">{{fi-icon}}</span></li>\
                                        </ul>\
                                        <ul class="list-inline pull-right">\
                                            <li><a class="icon-jfi-trash jFiler-item-trash-action"></a></li>\
                                        </ul>\
                                    </div>\
                                </div>\
                            </div>\
                        </li>',
            progressBar: '<div class="bar"></div>',

            itemAppendToEnd: false,

            removeConfirmation: true,
            _selectors: {
                list: '.jFiler-items-list',
                item: '.jFiler-item',
                progressBar: '.bar',
                remove: '.jFiler-item-trash-action'
            }
        },

        dragDrop: {
            dragEnter: null,
            dragLeave: null,
            drop: null,
        },

        uploadFile: {
            url: "upload.php",
            data: null,
            type: 'POST',
            enctype: 'multipart/form-data',
            beforeSend: function(){},
            success: function(data, el){
                var parent = el.find(".jFiler-jProgressBar").parent();
                el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
                    $("<div class=\"jFiler-item-others text-success\"><i class=\"icon-jfi-check-circle\"></i> Success</div>").hide().appendTo(parent).fadeIn("slow");    
                });
            },
            error: function(el){
                var parent = el.find(".jFiler-jProgressBar").parent();
                el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
                    $("<div class=\"jFiler-item-others text-error\"><i class=\"icon-jfi-minus-circle\"></i> Error</div>").hide().appendTo(parent).fadeIn("slow");    
                });
            },
            statusCode: null,
            onProgress: null,
            onComplete: null
        },
        files: null,
        addMore: false,
        clipBoardPaste: true,
        excludeName: null,
        beforeRender: null,
        afterRender: null,
        beforeShow: null,
        beforeSelect: null,
        onSelect: null,
        afterShow: null,
        onRemove: function(itemEl, file, id, listEl, boxEl, newInputEl, inputEl){
            var file = file.name;
            $.post('./php/remove_file.php', {file: file});
        },
        onEmpty: null,
        options: null,
        captions: {
            button: "Choose Files",
            feedback: "Choose files To Upload",
            feedback2: "files were chosen",
            drop: "Drop file here to Upload",
            removeConfirmation: "Are you sure you want to remove this file?",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only Images are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });

});

1 个答案:

答案 0 :(得分:0)

我发现使用Dropzone更好的方法。谢谢你