Php重定向无法提交

时间:2017-10-20 10:29:42

标签: php wordpress csv

一旦通过单击提交按钮生成了csv,我就会尝试重定向到另一个页面。到目前为止,当我点击按钮时它会发送CSV,它会给我一条消息“成功”,这让我觉得它运作良好。

然而,我真的希望页面在提交后重定向。我在else语句中添加了header(location:),但它没有做任何事情。

有人能帮帮我吗?

    if ( isset( $_POST['submit'] ) ) {

    function send_csv_mail ($csvData, $body, $to = 'email@email.co.uk', $subject = 'Test email with attachment', $from = 'webmaster@example.com') {

    global $post;
    $user_id = get_current_user_id();

      // This will provide plenty adequate entropy
      $multipartSep = '-----'.md5(time()).'-----';

      // Arrays are much more readable
      $headers = array(
        "From: $from",
        "Reply-To: $from",
        "Content-Type: multipart/mixed; boundary=\"$multipartSep\""
      );

      // Make the attachment
      $attachment = chunk_split(base64_encode(create_csv_string($csvData))); 

      // Make the body of the message
      $body = "--$multipartSep\r\n"
            . "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
            . "Content-Transfer-Encoding: 7bit\r\n"
            . "\r\n"
            . "$body\r\n"
            . "--$multipartSep\r\n"
            . "Content-Type: text/csv\r\n"
            . "Content-Transfer-Encoding: base64\r\n"
            . "Content-Disposition: attachment; filename=\"".date('Y-m-d')."-".str_replace(' ', '-', strtolower(get_user_meta($user_id, "wpcf-branch-active", true)))."-file.csv\"\r\n"
            . "\r\n"
            . "$attachment\r\n"
            . "--$multipartSep--";

       // Send the email, return the result
       return @mail($to, $subject, $body, implode("\r\n", $headers)); 

    }

    $array = array(
                    array("Code", "Product Category", "Product Family", "Description", "Quantity", "Bay"),          
    );



    send_csv_mail($array, "Hello World!!!\r\n This is simple text email message.");



    if(!send_csv_mail) {
         echo "Error"; 
    }
    else {
        echo "success";
   Header("Location: somewhere.php");
    die();
    }



    }

4 个答案:

答案 0 :(得分:1)

您无法将数据发送到客户端/缓冲区,然后设置标头,因为标头已经发送。 删除echo "success";即可。 您也可以使用ob_clean()清除缓冲区,请在此处查看:http://php.net/manual/en/function.ob-clean.php

答案 1 :(得分:1)

我认为这是我一直使用的最佳解决方案: 这是一个常见问题: 使用HTML重定向,如下所示:

echo "<meta http-equiv=\"refresh\" content=\"0; url=./somewhere.php\" />";

如果你这样使用。您还可以设置重定向时间,如果您想等待1秒,您可以像下面这样写。

 echo "<meta http-equiv=\"refresh\" content=\"1; url=./somewhere.php\" />";

答案 2 :(得分:0)

您可以使用WordPress重定向功能

(member.toObject() as MyTestObject).name = member.name

答案 3 :(得分:0)

另一种方法是在成功后结束PHP标记,然后使用:

<script>
location.replace("somewhere.php");
</script>

然后拿起你的PHP标签来抓住你的右括号。

虽然这是JavaScript,但出于您的目的,它每次都会非常出色。