如果使用php变量成功上传DropzoneJS图像,则重定向到另一页 [英] Redirect to another page if DropzoneJS image upload is success with php variable
问题描述
我在表单中使用dropzoneJS。该表格还记录用户输入。下面的代码显示了我的简单操作。一切工作正常,但php变量未获得其价值。有点像这样
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
}
当单击表单中的提交按钮时,发生重定向,但是$ imgID没有得到它的值
When submit button in form is clicked, redirection is happening but $imgID is not getting its value
这是Javascript
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.
推荐答案
您可以将ID作为响应发送回浏览器,并在成功事件发生时使用dropzone
You can send the id back to the browser as response and take it with dropzone on success event like this.
php :(如果此文件用于处理其他请求,则可能是这样的结构)
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);
}
}
}
这篇关于如果使用php变量成功上传DropzoneJS图像,则重定向到另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!