douxuqiao6394 2014-01-25 21:58
浏览 106

使用jQuery完成提交后重定向

Im working on a progress status bar for a form... you can see a an example here: http://www.enteratenorte.org/app-example/exa/

This is what I have so far:

The css is:

<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
.progress { position:relative; width:652px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { width:0%; height:40px; border-radius: 3px; background-image:url('loader.gif')}
.percent { line-height:35px; color:#fff; position:absolute; display:inline-block; top:3px; left:48%; font-family:Verdana, Geneva, sans-serif; font-weight:bold; text-shadow: 2px 0 0 #009303, -2px 0 0 #009303, 0 2px 0 #009303, 0 -2px 0 #009303, 1px 1px #009303, -1px -1px 0 #009303, 1px -1px 0 #009303, -1px 1px 0 #009303; }
</style>

The HTML code is:

<h1>File Upload Progress Demo #1</h1>
        <form action="zip-upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file" accept="image/jpg, image/jpeg" /><br>
        <input type="submit" value="Upload File to Server">
    </form>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

The Script goes like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
    <script>
    (function() {

    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');

    $('form').ajaxForm({
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        success: function() {
            var percentVal = '100%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        complete: function(xhr) {
            status.html(xhr.responseText);
        }
    }); 

    })();       
    </script>

and the PHP in the Receiver file is:

if ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000000)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "There was an error uploading the file, please try again!";
    } else {
    move_uploaded_file($_FILES["file"]["tmp_name"],"temp/". $_FILES["file"]["name"]);
      chmod("temp/". $_FILES["file"]["name"], 0777);
      rename("temp/". $_FILES["file"]["name"], "temp/mynewfile.jpg");
      echo "Image successfully uploaded";     
    }
  }
else
  {
  echo "File doesn't match the requiere criteria";
  }

Everything is working perfectly. The problem is, when if finishes uploading, it only displays que "Image successfully uploaded", but I would like to redirect the form to a different page, how can this be done?

Also, progress bar works with Safari, Firefox and Chrome... surprisingly (I'm being sarcastic) it doesn't work in IE, it uploads but progress bar doesn't move until the file has been completely uploaded and it does displays the "Image successfully uploaded"

Can some one help me with the redirect option, if not too much abuse of assistance , also see what IE. :D

  • 写回答

2条回答 默认 最新

  • drg5577 2014-01-25 22:06
    关注

    For the redirect, you can just use header:

    header('Location:<URL>');

    But you need to remove the 'image sucessfully loaded' echo, header won't work if anything has been output before it.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况