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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址