-1

I have a php file that submits a form. I type into the index.html, and then it send the info to email.php. I am trying to redirect back to index.html after the form is submitted but I am getting the error: "Warning: Cannot modify header information - headers already sent by"

I have tried using header("Location:http://www.mywebpage.com/index.html"); at every different line of the php file and it hasn't worked. Below is my code. Any ideas?:

<?php

$filename = "email.csv";
$exists = (file_exists($filename));

$handle = fopen($filename, 'a');
$msg = "Thank you for your appointment.\n";//EMail message
$stringToAdd="";    //File into

if (!$exists){
    foreach($_POST as $name => $value) {
        $stringToAdd.="$name,";
    }
    $stringToAdd.="\n";
    $new=False;
    fwrite($handle, $stringToAdd);
}

$stringToAdd="";

foreach($_POST as $name => $value) {
    print "$name : $value<br>";
    $msg .="$name : $value\n";
    $stringToAdd.="$value,";
}

$stringToAdd.="\n";

fwrite($handle, $stringToAdd);
//now close the file
fclose($handle); 
$to = $_POST["uniqname"]."@tobey.com";
$headers = "From: ". "UMSI CDO" ."<umsi.careers@tobey.com>\r\n";

mail($to, 'Appointment Scheduled', $msg, $headers); // 'Appointment Scheduled' is the subject

echo "Email sent";
?>
Sam
  • 7,252
  • 16
  • 46
  • 65
kegewe
  • 291
  • 1
  • 4
  • 14
  • I know, I looked over that question and tried to implement what they were saying, but was still unsuccessful – kegewe Apr 26 '14 at 18:50
  • 1
    There is no where I see that is outputing to the browser but the last line...do you have a space at the start of the file? Knowing the rest of the error would be helpful. – Thomas Apr 26 '14 at 18:51
  • It outputs all my form data and then prints Email sent. I want it to do that and then redirect to a new page – kegewe Apr 26 '14 at 18:53
  • I know that it has been asked before and I poured over that stackoverflow question before asking mine...I still think something is wrong though, because depending where I put the header() call, I either get that error, or it sends the email properly and ignores the header() call completely – kegewe Apr 26 '14 at 18:55
  • 1
    @kegewe: get rid of php closing tag `?>` and whitespaces,html,blank lines before php opening tag ` – potashin Apr 26 '14 at 18:58
  • Got rid of closing tag, there is no whitespace and I get the error: `Warning: Cannot modify header information - headers already sent by (output started at /home/email.php:20) in /home/email.php on line 32` This is because I put the header() call on line 32. The error is on whatever line I place the call, unless I place it high enough up in the code, at which point it is just completely ignored all together. – kegewe Apr 26 '14 at 18:59
  • PHP gave you a clue: _output started at /home/email.php:20_. What exactly is at line 20. And what the heck does `print` do? It is supposed to print something, isn't it? – Salman A Apr 26 '14 at 19:21
  • You have `print` in the second `foreach` loop. – TheFrost Apr 26 '14 at 19:22

2 Answers2

0

Get rid of php closing tag ?> and whitespaces, html, blank lines before php opening tag <?php. Also check if there is no output before :

header("Location:http://www.mywebpage.com/index.html");

Like print,var_dump, echo and so on.

potashin
  • 44,205
  • 11
  • 83
  • 107
0

You need to remove this print "$name : $value<br>";, as you cannot output anything before calling header()

Zebra North
  • 11,412
  • 7
  • 37
  • 49