简体   繁体   English

使用jQuery完成提交后重定向

[英]Redirect after submit has been done using 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/ 我正在处理表单的进度状态栏...您可以在此处查看示例: http : //www.enteratenorte.org/app-example/exa/

This is what I have so far: 这是我到目前为止的内容:

The css is: CSS是:

<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: HTML代码是:

<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: 并且Receiver文件中的PHP为:

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" 此外,进度条可与Safari,Firefox和Chrome配合使用...令人惊讶的是(我很讽刺)它在IE中不起作用,它可以上传,但是进度条在文件完全上传并显示之前不会移动“图片成功上传”

Can some one help me with the redirect option, if not too much abuse of assistance , also see what IE. 有人可以通过重定向选项帮助我,如果没有太多滥用帮助,还可以查看IE。 :D :d

Hopefully I understand your question correctly and am answering the right question... Off the top of my head, there are at least three ways to redirect: 希望我能正确理解您的问题并回答正确的问题...头顶上方,至少有三种重定向方法:

PHP: PHP:

header("Location: pagename.php");

HTML: HTML:

<meta HTTP-EQUIV="REFRESH" content="5; url=pagename.php">

The PHP header() method is preferred, but will not work if other headers (or html) were previously output to page. 最好使用PHP header()方法,但是如果以前将其他标头(或html)输出到页面,则该方法将不起作用。 As a work-around, you can use this method to redirect the page, and the bonus is that it will wait the specified number of seconds before doing so (5 in this case, or zero if you choose). 解决方法是,您可以使用此方法重定向页面,其好处是它将等待指定的秒数(在这种情况下为5,在这种情况下为零)。

JS: JS:

window.location.href = 'http://example.com';  // or just 'pagename.php';

For the redirect, you can just use header : 对于重定向,您可以只使用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. 但是您需要删除“已成功加载映像”回显,如果在此之前没有输出任何内容,则标头将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM