在使用 PHP 上传 Dropzone JS 文件期间调用 PHP 中的 Javascript 函数
Call Javascript function in PHP during Dropzone JS file upload with PHP
我正在尝试在 PHP 文件上传中调用 Javascript 函数。 PHP 方法取自此处:
<?php
$post_id = $_GET['post_id'];
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '/images/$post_id';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
echo "<script type='text/javascript'>showfeaturedimg('$post_id');</script>";
}
?>
文件上传没有问题。
我尝试了多种方法,我在不同的论坛上找到了这些方法,但是 none 其中有效:
echo "<script language='javascript'>showfeaturedimg('$post_id');</script>";
or
echo "<script>showfeaturedimg(\''$post_id'\');</script>";
showfeaturedimg() 函数只需调用 AJAX 即可从数据库中检索存储的图像并将其显示在页面上,而无需重新加载页面。
此外还尝试添加一个 javascript 函数调用到 Dropzone 的 queuecomplete,但是也不起作用:
<form action="upload-post.php?post_id=<?php echo $post_id;?>" class="dropzone" id="news-dropzone">
<div class="fallback">
<input name="file" type="file" multiple="multiple" />
</div>
</form>
<script>
Dropzone.options.dropzoneJsForm = {
init: function () {
this.on("success", function (file) {
alert("All files have uploaded ");
});
}
};
</script>
我做错了什么?
首先,您需要将带有 dropzone 配置的脚本放入主体中,否则 dropzone 将自动检测您的表单,并使用默认选项创建它。
然后在成功事件中,您可以在 file.xhr.response
中访问服务器响应,或者如果您传递第二个参数将成为响应本身,在此示例中,警报将显示您在 [= =24=] 文件.
<body>
......
<script>
Dropzone.options.newsDropzone= {
init: function () {
this.on("success", function (file, response) {
alert(response); // or alert(file.xhr.response);
})
}
};
</script>
</body>
如果您想执行响应而不是在警报中显示它,只需将其附加到 html,任何地方都可以。
或者,如果您愿意,您可以使用 jQuery 插件来配置 dropzone,但为了首先执行此操作,您需要将自动发现设置为 false,这里有一个示例:
<script>
Dropzone.autoDiscover = false;
$('#news-dropzone').dropzone({
init: function () {
this.on("success", function (file, response) {
alert(response); // or alert(file.xhr.response);
})
}
});
</script>
注意:还要检查控制台,因为我认为您的初始代码应该会出错。
我正在尝试在 PHP 文件上传中调用 Javascript 函数。 PHP 方法取自此处:
<?php
$post_id = $_GET['post_id'];
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '/images/$post_id';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
echo "<script type='text/javascript'>showfeaturedimg('$post_id');</script>";
}
?>
文件上传没有问题。
我尝试了多种方法,我在不同的论坛上找到了这些方法,但是 none 其中有效:
echo "<script language='javascript'>showfeaturedimg('$post_id');</script>";
or
echo "<script>showfeaturedimg(\''$post_id'\');</script>";
showfeaturedimg() 函数只需调用 AJAX 即可从数据库中检索存储的图像并将其显示在页面上,而无需重新加载页面。
此外还尝试添加一个 javascript 函数调用到 Dropzone 的 queuecomplete,但是也不起作用:
<form action="upload-post.php?post_id=<?php echo $post_id;?>" class="dropzone" id="news-dropzone">
<div class="fallback">
<input name="file" type="file" multiple="multiple" />
</div>
</form>
<script>
Dropzone.options.dropzoneJsForm = {
init: function () {
this.on("success", function (file) {
alert("All files have uploaded ");
});
}
};
</script>
我做错了什么?
首先,您需要将带有 dropzone 配置的脚本放入主体中,否则 dropzone 将自动检测您的表单,并使用默认选项创建它。
然后在成功事件中,您可以在 file.xhr.response
中访问服务器响应,或者如果您传递第二个参数将成为响应本身,在此示例中,警报将显示您在 [= =24=] 文件.
<body>
......
<script>
Dropzone.options.newsDropzone= {
init: function () {
this.on("success", function (file, response) {
alert(response); // or alert(file.xhr.response);
})
}
};
</script>
</body>
如果您想执行响应而不是在警报中显示它,只需将其附加到 html,任何地方都可以。
或者,如果您愿意,您可以使用 jQuery 插件来配置 dropzone,但为了首先执行此操作,您需要将自动发现设置为 false,这里有一个示例:
<script>
Dropzone.autoDiscover = false;
$('#news-dropzone').dropzone({
init: function () {
this.on("success", function (file, response) {
alert(response); // or alert(file.xhr.response);
})
}
});
</script>
注意:还要检查控制台,因为我认为您的初始代码应该会出错。