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.

    评论

报告相同问题?

悬赏问题

  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图