Reputation: 811
I'm trying to redirect to another page once a csv has been generated by a clicking the submit button. So far, when i click the button it sends the CSV and it gives me the message "Success" which makes me think it's working well.
However, what i really want the page to redirect after the submission has been made. I've added header(location: ) within the else statement but it's not doing anything.
Can anyone help me please?
if ( isset( $_POST['submit'] ) ) {
function send_csv_mail ($csvData, $body, $to = '[email protected]', $subject = 'Test email with attachment', $from = '[email protected]') {
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();
}
}
Upvotes: 0
Views: 72
Reputation: 666
I think this is the best solution I always use it: This is a common problem: use HTML redirect like below:
echo "<meta http-equiv=\"refresh\" content=\"0; url=./somewhere.php\" />";
If you use this way. you can also set time to redirect like if you want to wait for 1 sec you can write like below.
echo "<meta http-equiv=\"refresh\" content=\"1; url=./somewhere.php\" />";
Upvotes: 1
Reputation: 1884
An alternative is to end your PHP tag after success, and then use:
<script>
location.replace("somewhere.php");
</script>
and then pick up your PHP tag to catch your closing brackets.
While this is JavaScript, for your purposes, it will work brilliantly everytime.
Upvotes: 0
Reputation: 465
You can't send data to the client/buffer and then set an header, because the header was already sent.
Remove echo "success";
and it will work.
You can use ob_clean()
to clean the buffer too, check here: http://php.net/manual/en/function.ob-clean.php
Upvotes: 1