表单提交后的重定向在IE中不起作用,但在chrome中起作用

时间:2019-01-11 14:01:11

标签: php forms redirect

我创建了一个发件人,提交成功后将显示“谢谢”页面。 成功是在验证表单字段并且是否有文件上传到服务器之后。

在所有浏览器中,消息和附件均正确发送-我收到了电子邮件到收件箱,但是,在FireFox和Chrome浏览器中,提交成功后,我被重定向到“谢谢”页面。 在IE 11中,它根本没有重定向。

此页面的链接为: Link to my form

我使用ob_start(); &ob_flush();只是为了安全起见,在“谢谢”页面上。

该表格已定义:

/ _ scripts / uploadAndSend.php“ enctype =” multipart / form-data“>

这是提交功能:

$('form[name="formmail"]').submit(function (event) {
  "use strict";
  event.preventDefault(); //prevent default action 
  if (!checkForm()) {
    return false;
  } else {
    console.log("passed check");
    var post_url = $(this).attr("action"); //get form action url
    var request_method = $(this).attr("method"); //get form GET/POST method
    //form_data.append('language', $('input[name=language]')[0]);

    // Attach file
    var form_data = new FormData();
    var myform = $('form[name="formmail"]'); // specify the form element
    //var myform = $("#formmail"); // specify the form element
    var idata = myform.serializeArray();
    var file_data = $("#browseButton").prop("files")[0];

    form_data.append("fileatt", file_data);

    $.each(idata, function (key, input) {
      form_data.append(input.name, input.value);
    });
    //alert(form_data);
    $.ajax({
      url: post_url,
      //type: "POST",
      //dataType:'json',
      type: request_method,
      data: form_data,
      contentType: false,
      processData: false,
      xhr: function () {
        //upload Progress

        var xhr = $.ajaxSettings.xhr();
        if (xhr.upload) {
          xhr.upload.addEventListener("progress", function (event) {

            var percent = 0;
            var position = event.loaded || event.position;
            var total = event.total;
            if (event.lengthComputable) {
              percent = Math.ceil(position / total * 100);
            }
            //update progressbar
            $(".progress-bar").css("width", percent + "%");
            $(".progress-bar").html(percent + "%");
            //$("#upload-progress .progress-bar").css("width", + percent +"%");
          }, true);
        }
        return xhr;
      }
    }).done(function (response) { //
      $("#server-results").html(response);
    });
  }
});

////////// 重定向到“谢谢”页面发生在这里:

<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<?php 
    $host = $_SERVER['HTTP_HOST'];
    if(strpos($host, "dnd-production.com") !== false){  
        $site_Root = "http://$host";
    } else {
        $uri   = explode("/", $_SERVER['PHP_SELF']);
        //print_r($uri);
        $site_Root = "//$host/$uri[1]/";
    }

    if(isset($_GET['lang'])) {
      $lang = $_GET['lang'];
    } else {
      $lang = "he"; 
    }
    if($lang == "en") {
        $thankYouPage = "${site_Root}/_english/thankyou.php?page=null";
    } else {
        $thankYouPage = "${site_Root}/_pages/thankyou.php";
    }
    echo "<meta http-equiv=\"refresh\" content=\"0; url=$thankYouPage\">";
?>

<title>Email Form</title>
</head>

<body onLoad="Refresher(1)">
<?php

$to = 'info@dnd-production.com';

$name           = $_POST['name'];
$email          = $_POST['email'];
$subject        = $_POST['subject'];
$comments   = $_POST['message'];
$phone          = $_POST['phone'];
$from           = $email;
$lang           = $_POST['language'];

// Get html message content
$form_data  = "<p>This email is from <span class=\"bold\">$name</span></p> \n\n ";
$form_data .= "<p>Phone: $phone</p>";
$form_data .= "<p>Email: $email</p>";
$form_data .= "<h3>$subject</h3>";
$form_data .= "<p>$comments</p>";
$message =    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" .
                  "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" .
                  "<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" .
                  "<head> \n" .
                  "  <meta http-equiv=\"content-type\" content= \n" .
                  "  \"text/html; charset=UTF-8\" /> \n" .
                  "<style type=\"text/css\"> \n" .
                  "body {    font-size: 9pt; font-family:  verdana, sans-serif;     color: #000; background:#fff; }  \n" .
                  ".bold { font-weight: bold; }  \n" .
                  "</style>  \n" .
                  "</head> \n" .
                  "<body>$form_data \n" .
                  "</body> \n" .
                  "</html> \n\n";

$headers = "From: $from";
// Obtain file upload vars
if(isset($_FILES['fileatt'])){  
  $fileatt      = $_FILES['fileatt']['tmp_name'];
  $fileatt_type = $_FILES['fileatt']['type'];
  $fileatt_name = $_FILES['fileatt']['name'];
  $fileatt_size = $_FILES['fileatt']['size']/1024;//size in KBs

  if (is_uploaded_file($fileatt)) {
    // Read the file to be attached ('rb' = read binary)
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    // Generate a boundary string
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    // Add the headers for a file attachment
    $headers .= "\nMIME-Version: 1.0\n" .
             "Content-Type: multipart/mixed;\n" .
             " boundary=\"{$mime_boundary}\"";
    // Add a multipart boundary above the html message
    $message = "This is a multi-part message in MIME format.\n\n" .
           "--{$mime_boundary}\n" .
           "Content-Type: text/html; charset=\"UTF-8\"\n" .
           "Content-Transfer-Encoding: 7bit\n\n" .
           $message . "\n\n";

    // Base64 encode the file data
    $data = chunk_split(base64_encode($data));
    //We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:
    //Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              "Content-Disposition: attachment;\n" .
              " filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
  }
}
//end if is_uploaded_file
else {
  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";
  // Add a multipart boundary above the html message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/html; charset=\"UTF-8\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";
}                        
mail($to, $subject, $message, $headers);
?>
</body>
</html>
<? ob_flush(); ?>

2 个答案:

答案 0 :(得分:1)

为什么不使用标头进行重定向?

代替

if($lang == "en") {
        $thankYouPage = "${site_Root}/_english/thankyou.php?page=null";
    } else {
        $thankYouPage = "${site_Root}/_pages/thankyou.php";
    }
    echo "<meta http-equiv=\"refresh\" content=\"0; url=$thankYouPage\">";

使用:

if($lang == "en") {
    $thankYouPage = "${site_Root}/_english/thankyou.php?page=null";
} else {
    $thankYouPage = "${site_Root}/_pages/thankyou.php";
}
header('Location: ' . $thankYouPage);
 exit();

答案 1 :(得分:0)

尝试一下:

echo '<meta http-equiv="refresh" content="0;URL='.$thankYouPage.'" />';

对于旧版本,请使用:

echo '<meta http-equiv="refresh" content="0;URL='.$thankYouPage.'" /> </meta>';