Question : PHP  Warning: Cannot modify header information

When I fill in the form in gatherInfo.php, I get this error message when it goes to contact3.php. However, on at least one occasion when I tested it, it did email me the data from the form. Naturally, I never get redirected to thankyou.html

Warning: Cannot modify header information - headers already sent by (output started at /home/discrete/public_html/Test/contact3.php:10) in /home/discrete/public_html/Test/contact3.php on line 10

What do you see in the code for contact3.php that causes these errors and how do I fix them?

Thanks,
John

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
CODE FOR contact3.php

<?php $to = $_REQUEST['sendto'] ; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Company"} = "Company"; $fields{"Email"} = "Email"; $fields{"Phone"} = "Phone"; $fields{"list"} = "Mailing List"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ 	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: [email protected]"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com"; if($from == '') {print "You have not entered an email, please go back and try again";} else { if($name == '') {print "You have not entered a name, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {header( "Location: http://www.discretedata.com/Test/thankyou.html" );} else {print "We encountered an error sending your mail, please notify [email protected]"; } } } ?> 


***********************************

CODE FOR gatherInfo.php
<form method="post" action="contact3.php"> 
<table bgcolor=#ffffcc align=center> 
<tr><td colspan=2><strong>Contact us using this form:</strong></td></tr> 
<tr><td>Department:</td>
<td><select name="sendto"> <option value="[email protected]">General</option> <option value="[email protected]">Support</option> <option value="[email protected]">Sales</option> </select></td>
</tr> 
<tr><td><font color=red>*</font> Name:</td>
<td><input size=25 name="Name"></td>
</tr> 
<tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td>
</tr> 
<tr><td>Company:</td>
<td><input size=25 name="Company"></td>
</tr> <tr><td>Phone:</td><td><input size=25 name="Phone"></td></tr> <tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr> <tr><td colspan=2>Message:</td></tr> <tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr> <tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr> <tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr> </table> </form>

Answer : PHP  Warning: Cannot modify header information

In order to use the "header()" function in line 26 below, the PHP code Must come first with not even a space before it.  Even a space before it will be sent to the browser and then headers Can't be sent.  "$headers" is a variable to hold the 'header' info for the email message.  The "header" from the "header()" function is the one that the server sends to the browser to tell it what is coming.  'header' in both cases refers to something Before the actual message or information.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
<?php 
$to = $_REQUEST['sendto'] ; 
$from = $_REQUEST['Email'] ; 
$name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$subject = "Web Contact Data"; 
$fields = array(); 
$fields{"Name"} = "Name"; 
$fields{"Company"} = "Company"; 
$fields{"Email"} = "Email"; 
$fields{"Phone"} = "Phone"; 
$fields{"list"} = "Mailing List"; 
$fields{"Message"} = "Message"; 
$body = "We have received the following information:\n\n"; 
foreach($fields as $a => $b){ 	
	$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
	$headers2 = "From: [email protected]"; 
	$subject2 = "Thank you for contacting us"; 
	$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com"; 
	if($from == '') {$ermsg = "You have not entered an email, please go back and try again";} 
	else { 
		if($name == '') {$ermsg = "You have not entered a name, please go back and try again";} 
		else { 
			$send = mail($to, $subject, $body, $headers); 
			$send2 = mail($from, $subject2, $autoreply, $headers2); 
			if($send) {header( "Location: http://www.discretedata.com/Test/thankyou.html" );} 
			else {$ermsg = "We encountered an error sending your mail, please notify [email protected]"; }
		} 
	} 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php 
echo $errmsg;
 ?>
</body>
</html>
Random Solutions  
 
programming4us programming4us