PDA

View Full Version : Headers already sent problem



wkenny
04-06-2008, 04:48 PM
I am trying to set up a form in PHP with built-in validation. If validation passes I want to send an email with the form contents and then go to a thank you page. If there is an error I reload the form page and highlight any problems.

But once validation passes and the mail is sent I get the error

Cannot modify header information - headers already sent by ...
followed by the line number with my call to mail()

My code is
mail("info@mysite.com", "The Subject ",$rmessage);
header ("Location: http://www.mysite.com/querytku.htm");

How do I get around this?

thetestingsite
04-06-2008, 05:21 PM
What's the code before that line? Are you outputting any text to the browser before the mail function? Please post the full code so that we can see what's going on.

wkenny
04-06-2008, 06:28 PM
Yes, it outputs to browser first. Here is the full code

<?
$message = "";
$errormessage = "";
$nameclass = "formprompt";
$promptclass = "formprompt";
$emailclass = "formprompt";
$checkerclass = "formprompt";
$questionsclass = "formprompt";
$visitor = "";
$email = "";
$notes = "";
$attn = "";
$check="";

if (isset($_POST['process'])) {
if ($_POST['process'] == 1) {

$visitor = $_POST['your_name'];
$email = $_POST['email'];
$notes = $_POST['questions'];
$attn = $_POST['con_phone'];
$check=$_POST['checker'];


if (eregi('http:', $notes)) {
$errormessage="Form not completed correctly";
$questionclass="formprompterr";
}
if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{
$errormessage="Form not completed correctly";
$emailclass="formprompterr";

}

if(empty($visitor)) {
$errormessage="Form not completed correctly";
$nameclass="formprompterr";
}

if(empty($email)) {
$errormessage="Form not completed correctly";
$emailclass="formprompterr";
}

if(empty($notes )) {
$errormessage="Form not completed correctly";
$questionsclass = "formprompterr";
}
if(empty($check )) {
$errormessage="Form not completed correctly";
$checkerclass = "formprompterr";
}
if($check!=12) {
$errormessage="Form not completed correctly";
$checkerclass = "formprompterr";
}

$notes = stripcslashes($notes);
if($errormessage=="") {
$message= "Email: ".$email." Name: ".$visitor." Questions: ".$notes." Phone: ".$attn;
mail("info@estateagentsespana.com", "Arroyo bar for sale Eng ",$message);
header ("Location: http://www.estateagentsespana.com/querytku.htm");
}


}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Three apartments and two retail units for sale in Benalmadena, Costa del Sol, Spain</title>
<meta name="description" content="Building for Sale in Arroy de la Miel, Costa del Sol, Malaga">
<meta name="keywords" content="building, for sale, apartments, retail units, arroyo de la miel, benalmadena, costa del sol, spain">
<link rel="stylesheet" type="text/css" href="ncssstyles.css" id="sitestyles">
<link rel="stylesheet" type="text/css" href="proppage.css" id="sitestyles">
<script type = "text/javascript" src="bookmarksite.js"></script>
<script type = "text/javascript" src="maxwin.js"></script>
<script type="text/javascript" src="javas/beekay.js"></script>
</head>

<body>
<div id="container">
<div id="top">
<div id="hleftnav">
<img src="images/eae.jpg">
</div><!-- hleftnav -->
<div id="hrightnav">
</div><!-- hrightnav -->


<div id="hcontent">

<h1>Leasehold bar for sale in Arroyo de la Miel, Benalmadena, Costa del Sol</h1>
</div><!-- hcontent -->


</div> <!-- htop -->
<div>
<ul class="solidblockmenu">
<li><a href="index.html" target="_self" >Home</a></li>
<li><a href="mortgages.htm" target="_self" >Mortgages</a></li>
<li><a href="property_for_sale_in_spain.htm" target="_self" >Find a Property</a></li>
<li><a href="register.htm" target="_self" >Register</a></li>
<li><a href="aboutus.htm" target="_self" >Contact us</a></li>
<li><a href="advertise.htm" target="_self" >Advertise</a></li>
<li><a href="info.htm" target="_self" >Buyer's Guide</a></li>
<li><a href="links.htm" target="_self" >Links</a></li>
<li><a href="javascript:bookmarksite('Estate Agents Spain Directory', 'http://www.estateagentsespana.com/')">Bookmark Site</a></li>
</ul>
</div><!-- top nav -->
<br style="clear: left" />

<div id="leftnav">
<ul id="navigation">


</ul>

<br />
<br />

</div> <!-- leftnav -->
<div class="content">



<div class="txtcontent" align="center">
<h1>Leasehold Bar in Arroyo de la Miel, Benalmadena, Costa del Sol</h1>
<p>If you would like to arrange a viewing or have any other questions, please complete the form below. Alternatively, email us on </p><script language="JavaScript" type="text/javascript">
<!--
Beekay:vee(1);//--></script> <br />

<br />



<div class="formholder">
<div class="formprompterr">
<? print $errormessage; ?>
</div>
<div style="clear:both"></div>

<form action="contact2.php" method="post" name="arroyobuilding">

<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>


<div class="<?print $emailclass; ?>">
Contact E-Mail:
</div>
<div class="forminput">

<input type="text" class="blocktext" id="P386056931TXR90" name="email"
style="width:80%;overflow:hidden" maxlength="100" value="<?print $email; ?>">
</div>
<div class="<?print $nameclass; ?>">
Your Name:
</div>
<div class="forminput">
<input type="text" class="blocktext" id="C386056944NVK92" name="your_name"
style="width:80%;overflow:hidden">
</div>
<div class="<?print $questionsclass; ?>">
Any specific questions
</div>
<div class="forminput">
<textarea class="plain-text" id="Z386057140JCG59" name="questions" rows="5" cols="20"
style="width:80%;">
</textarea>
</div>
<div class="formprompt">
Contact phone:
</div>
<div class="forminput">
<input type="text" class="plain-text" id="I386057242FEK28" name="con_phone"
style="width:30%;overflow:hidden" maxlength="20">
</div>
<div class="<?print $checkerclass; ?>">
What is six plus six:
</div>

<div class="forminput">
<input type="text" class="plain-text" id="I386057242FEK28" name="checker"
style="width:30%;overflow:hidden" maxlength="2">
</div>


<div class="formbutton">
<input type="submit" class="default-style" id="R386057246ZME29" name="submit" value="Send"
style="background-color:#B5B5B5; overflow:hidden">
</div>
<div class="formbutton">

<input type="reset" class="default-style" id="U386057253JYI31" name="reset" value="Clear"
style="background-color:#B5B5B5; overflow:hidden">
</div>
<input type="hidden" id="T386058791MYA61" name="recipient" value="info@estateagentsespana.com">

<input type="hidden" id="Y386058794LIP62" name="subject" value="Arroyo building for sale">

<input type="hidden" id="U386058799TNT63" name="redirect" value="querytku.htm">
<input type="hidden" name="process" value="1">


</form>
</div>

<script type="text/javascript" language="JavaScript">
var str=' & ';
function CheckTheForm(){
var errormessage = new String();
if(WithoutContent(document.arroyobar.email.value))
{errormessage += "\n\nPlease fill in the Contact E-mail field";}
if(NotEmail(document.arroyobar.email.value))
{errormessage += "\n\nThe entry for Contact E-mail does not appear to be a valid email address";}

if (errormessage.length > 2){
alert('Form not completed correctly:' + errormessage + '\n');
return false;
}
document.arroyobar.questions.value=rep(document.arroyobar.questions.value);
return true;
} // end of CheckTheForm()

function WithoutContent(ss){
if (ss.length>0){return false;}
return true;
}
function rep(t){
var t=t;
for (var i = 0; i < rep.ar.length; i++)
t=t.replace(rep.ar[i++], rep.ar[i]);
return t;
}
rep.ar=[
//unicode | entity/string
/\u0026/g, '&amp;', //the ampersand (&) must be first or else it will be replaced in other replacements
/\u00d1/g, '&#209;',
/\u00f1/g, '&#241;',
/\u00e1/g, '&#225;',
/\u00e9/g, '&#233;',
/\u00ed/g, '&#237;',
/\u00f3/g, '&#243;',
/\u00fa/g, '&#250;',
/\u00a3/g, 'sterling',
/\u20ac/g, 'euros' //note - no comma after last entry
];

function WithoutCheck(ss){
if(ss.checked){return false;}
return true;
}

function NotEmail(ss){
var splitted = ss.match("^(.+)@(.+)$");
if (ss.length == 0){return false;}
if(splitted == null) return true;
if(splitted[1] != null){
var regexp_user=/^\"?[\w-_\.]*\"?$/;
if(splitted[1].match(regexp_user) == null) return true;
}
if(splitted[2] != null){
var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null){
var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if(splitted[2].match(regexp_ip) == null) return true;
}
return false;
}
return true;
}

</script>

<div style="clear:both"></div>
</div>


<div id="rightnav"> <!-- right nav -->
<div class="googleblock">


</div>



</div>

<div style="clear:both">&nbsp;</div>
<div>
<ul class="solidblockmenu">
<li><a href="index.html" target="_self" >Home</a></li>
<li><a href="mortgages.htm" target="_self" >Mortgages</a></li>
<li><a href="property_for_sale_in_spain.htm" target="_self" >Find a Property</a></li>
<li><a href="register.htm" target="_self" >Register</a></li>
<li><a href="aboutus.htm" target="_self" >Contact us</a></li>
<li><a href="advertise.htm" target="_self" >Advertise</a></li>
<li><a href="info.htm" target="_self" >Buyer's Guide</a></li>
<li><a href="links.htm" target="_self" >Links</a></li>
<li><a href="javascript:bookmarksite('Estate Agents Spain Directory', 'http://www.estateagentsespana.com/')">Bookmark Site</a></li>
</ul>
</div><!-- top nav -->

<div id="footer">

</div>
<div style="clear:both">&nbsp;</div>
</div>
</div>
</body>
</html>