【问题标题】:Send dropzone with call function发送带有呼叫功能的 dropzone
【发布时间】:2021-02-28 20:00:58
【问题描述】:

我的问题是我必须将普通表单与 dropzone.js 结合起来进行拖放上传。用户单击提交按钮后,如果输入中有值,则 ajax 请求将数据发送到 php 脚本。

但我的原始表单调用 onSave 函数来验证输入数据。如果我使用下面的代码发送表单,POST 方法会完美发送数据,但不会调用 onSave。所以什么也没发生。

上传文件后如何修改代码以调用 onSave?

<?php

function onSave(){
    //validate and save to database
}

?>

<form method="POST" action="item/create" accept-charset="UTF-8" data-request="onSave" class="dropzone" enctype="multipart/form-data" id="mydropzone">
    //inputs

    <div id="dropzonePreview" class="dropzone dropzonePreview">
        <div class="dz-default dz-message"><button class="dz-button" type="button">Drop files here to upload</button></div>
    </div>

    //more inputs
    <button type="submit" class="btn btn-success save">Send</button>
</form>

<script type="text/javascript">

Dropzone.autoDiscover = false;

$( document ).ready(function () {

    Dropzone.autoDiscover = false;
    $("#mydropzone").dropzone({ 

        autoProcessQueue: false,
        uploadMultiple: true,
        previewsContainer: '#dropzonePreview',
        clickable: false,

        accept: function(file, done) {
            console.log("uploaded");
            done();
        },
        error: function(file, msg){
            alert(msg);
        },

        // The setting up of the dropzone
        init: function() {
            var myDropzone = this;

            this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
                e.preventDefault();
                e.stopPropagation();
                myDropzone.processQueue();
                //it's send form, but not call onSave function
            });

        }
    });
});
</script>

【问题讨论】:

    标签: javascript php request dropzone.js dropzone


    【解决方案1】:

    您是否尝试调用onSave() 函数?

    init: function() {
                var myDropzone = this;
    
                this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    myDropzone.processQueue();
                    //it's send form, but not call onSave function
                  <?php  onSave();  ?> // <----
                });
    

    【讨论】:

    • 但是onSave是一个php函数。
    猜你喜欢
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多