【问题标题】:Redirection with Header(Location:) is not working when image is uploaded with dropjoneJS使用 dropzone JS 上传图像时,使用 Header(Location:) 重定向不起作用
【发布时间】:2015-12-24 08:56:15
【问题描述】:

在我的表单中,我使用的是 dropzoneJS。我试图在我的数据库中成功提交用户数据和图像后重定向用户。这就是我试图做到的方式。 我的表格 -

if(!empty($_POST)){
$userID = addUser() //This function create new user and return user id
    if (!empty($_FILES)) {
    //I have function here which will submit user images in a directory an create image path in DB table
    }
HEADER('LOCATION: view.php?id='.$userID);//User redirected to their profile page
}//If ends

表格开始:-

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" class="dropzone form-horizontal" id="my-awesome-dropzone"  method="post"  enctype="multipart/form-data">
<input type blah.. blah..>
<input type blah.. blan..>
<!-- DropzoneJS div -->
<div class="dropzone-previews form-group" style="height: 350px;"></div>
  <div class="fallback col-xs-3">
     <input name="file" class="input-file form-control "type="file" multiple/>
  </div>
<button type="submit" id="submit-all" class="btn btn-primary">Submit</button>
</form>

然后是 DropzoneJS 设置

<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
autoProcessQueue: false,
blah.. blah
blah.. blah

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
       }
    });
  }
}
</script>

现在,当用户填写并提交表单(dropzone 字段中没有图片)时,用户数据会提交到我的数据库中,并且用户已成功重定向到他们的个人资料页面 (mywebsite.com/view.php?id=1)。这可以。

但问题是—— 当用户填写并提交表单(在 dropzone 字段中包含图像)时,用户数据和图像都成功提交到我的数据库中,因为所需的重定向没有发生。这不好。为什么没有发生重定向?

请指出哪里错了。

【问题讨论】:

    标签: php dropzone.js


    【解决方案1】:

    可能存在语法错误。试试这个:

    if(!empty($_POST)){
    $userID = addUser(); //This function create new user and return user id
        if (!empty($_FILES)) {
        //I have function here which will submit user images in a directory an create image path in DB table
        }
    header('Location: view.php?id='.$userID);//User redirected to their profile page
    }//If ends
    

    第二种重定向方式:

    header('Refresh: 5; URL='view.php?id='.$userID);
    

    第三种重定向方式:

    <?php echo '<meta http-equiv="Refresh" content="5; url='view.php?id='.$userID.'">';?>
    

    第四种重定向方式:

    <?php echo '<script type="text/javascript">setTimeout(function(){location.replace("view.php?id='.$userID.'");}, 5000);</script>';?>
    

    【讨论】:

    • 我也试过这个 - //Refresh page when all images are uploaded myDropzone.on("complete", function (file) { if (myDropzone.getUploadingFiles().length === 0 &amp;&amp; myDropzone.getQueuedFiles().length === 0) { var userid = "&lt;?php $userID; ?&gt;"; window.location.replace("/view.php?id="+ userid); } }); 现在页面正在重定向,但用户 ID 没有得到它的值,因此出现错误页面。我试过 - var userid = "";但这会使拖放图像字段停止工作
    • 试试这个变种:windows.location.replace('view.php?id=') ;
    • 不工作。每当我添加单词echo 时,dropzone 字段就会停止工作。我认为它正在破坏 js 代码。没有echo,例如&lt;?php $userID; ?&gt; 正在工作,但 userID 没有得到它的价值。我已经尝试了各种方法..双引号..单引号..带..不带分号。这是我的小项目的重要组成部分。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2013-08-04
    • 2017-08-13
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多