duangu8264 2015-12-30 15:22
浏览 62
已采纳

如果使用php变量DropzoneJS图像上传成功,则重定向到另一个页面

I am using dropzoneJS in my form. The form also record user input. Below code shows what I am doing in simple. Everything is working fine but php variable is not getting its value. It is somewhat like this

if (!empty($_FILES)) {
 $imgID = submitData()//This functions upload image and write image url in database and then return ID of the affected row
}

When submit button in form is clicked, redirection is happening but $imgID is not getting its value

Here is the Javascript

Dropzone.options.myAwesomeDropzone = {
 autoProcessQueue: false,
 etc.. etc ..
init: function() {
 var myDropzone = this;
 $("#submit-all").click(function (e) {
   e.preventDefault();
   e.stopPropagation();
   if (myDropzone.files.length) {
    myDropzone.processQueue(); // upload files and submit the form
    } else {
    $('#my-awesome-dropzone').submit(); // submit the form
    }
  });
// Refresh page when all images are uploaded
myDropzone.on("complete", function (file) {
     if (myDropzone.getUploadingFiles().length === 0 && myDropzone.getQueuedFiles().length === 0) {
          var idvar = '<?php $imgID; ?>';
    window.location.replace("/preview.php?id="+ idvar);
    }
  });
 }
}

Suggest me where I am doing wrong. Is there any alternative available.

  • 写回答

1条回答 默认 最新

  • douxin0251 2015-12-30 22:01
    关注

    You can send the id back to the browser as response and take it with dropzone on success event like this.

    php: (If this file is used to handle other requests a possible structure can be like this)

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && !empty($_FILES)) 
    {
        $imgID = submitData();
        echo $imgID;
    }
    else
    {
        // The rest of your php file shoul go in here.
    }
    

    js:

    Dropzone.options.myAwesomeDropzone = {
    
        // .........
    
        init: function() {
            // ........
    
            // On success refresh
            this.on("success", function (file) {
                var idvar = $.trim(file.xhr.response);
                window.location.replace("/preview.php?id=" + idvar);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿