Contact form header error

Discussion in 'PHP' started by buddingphp, Jun 11, 2008.

  1. #1
    Hi

    I'm finding this site usefull. Thanks.
    I've just resolved an issue with my form and I think this is the last hurdle.
    My contact form works well and I get my thank you webpage displaying ok when all the fields are filled out, but if at least one field is left blank and then I submit I get this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/crikle/public_html/allthatmatters/mailer.php:31) in /home/crikle/public_html/allthatmatters/mailer.php on line 52

    I think my header command is correct because I have used it with a different set of coding on another site, but it does'nt seem to like this set of coding I'm now using. (the header command is near the end of this code)
    Hope you can help:

    <?php
    if(isset($_POST['submit'])) {


    $to = "goodguy@btinternet.com";
    $subject = "Form";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];


    if ($email_field == "") {
    echo "<h4>Please enter your email address.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";


    }

    elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email))
    {
    echo "<h4>Your email address is invalid. Please try again.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }


    elseif ($name_field == "") {
    echo "<h4>Please enter your name.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }
    elseif ($message == "") {
    echo "<h4>Please enter your query.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }







    if( isset($_POST['check']) ) {
    foreach($_POST['check'] as $value) {
    $check_msg .= "Checked: $value\n";
    }
    }



    $body = "From: $name_field\n E-Mail: $email_field\n $check_msg Message: $message\n";

    {
    header( "Location: http://www.all-that-matters.com/thankyou.htm" );

    }

    mail($to, $subject, $body);


    } else {

    echo "Failed";

    }
    ?>
     
    buddingphp, Jun 11, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Add the below function immediately after the PHP start tag
    
    ob_start();
    
    PHP:
    so will look something like
    
    <?php
    ob_start();
    /*
    your code here!
    */
    ?>
    
    PHP:
    You should always send the headers BEFORE you output anything to the client.

    And also wrap your PHP code inbetween php tags because it improves reability
     
    rohan_shenoy, Jun 11, 2008 IP
  3. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    The problem is it's trying to redirect when there has been output sent to the browser (error msg saying fill in all feilds, ect) which means the redirect can't be set.

    Try this:

    if(isset($_POST['submit'])) {
    	$to = "goodguy@btinternet.com";
    	$subject = "Form";
    	$name_field = $_POST['name'];
    	$email_field = $_POST['email'];
    	$message = $_POST['message'];
    
    
    	if ($email_field == "") {
    		echo "<h4>Please enter your email address.</h4>";
    		echo "<a href='javascript:history.back(1);'>BACK</a>";
    	}
    	elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email))
    	{
    		echo "<h4>Your email address is invalid. Please try again.</h4>";
    		echo "<a href='javascript:history.back(1);'>BACK</a>";
    	}
    	elseif ($name_field == "") {
    		echo "<h4>Please enter your name.</h4>";
    		echo "<a href='javascript:history.back(1);'>BACK</a>";
    	}
    	elseif ($message == "") {
    		echo "<h4>Please enter your query.</h4>";
    		echo "<a href='javascript:history.back(1);'>BACK</a>";
    	}
    	else
    	{
    
    		if( isset($_POST['check']) ) {
    			foreach($_POST['check'] as $value) {
    				$check_msg .= "Checked: $value\n";
    			}
    		}
    
    		$body = "From: $name_field\n E-Mail: $email_field\n $check_msg Message: $message\n";
    
    		mail($to, $subject, $body);
    		header( "Location: http://www.all-that-matters.com/thankyou.htm" );
    	}
    
    
    } else {
    	echo "Failed";
    }
    PHP:
    It only tries to redirect & send the email if there are no errors.

    Sky22 :)
     
    sky22, Jun 11, 2008 IP
  4. buddingphp

    buddingphp Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks it worked except....my form field validation code is now ineffective.
    I may need to re-locate your code?


    this is the validation code that is now ineffective:

    if ($email_field == "") {
    echo "<h4>Please enter your email address.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";


    }

    elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email))
    {
    echo "<h4>Your email address is invalid. Please try again.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }


    elseif ($name_field == "") {
    echo "<h4>Please enter your name.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }
    elseif ($message == "") {
    echo "<h4>Please enter your query.</h4>";
    echo "<a href='javascript:history.back(1);'>BACK</a>";
    }
     
    buddingphp, Jun 11, 2008 IP
  5. buddingphp

    buddingphp Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry this reply is not for you lol.
     
    buddingphp, Jun 11, 2008 IP
  6. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the reason the verification isn't working is because ob_start(); is caching the output (errors) and the redirect is sending the user to the new page before the cache (errors) is shown to the user.

    Sky22
     
    sky22, Jun 11, 2008 IP
  7. buddingphp

    buddingphp Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    this has worked brilliantly thanks all...php forever!
     
    buddingphp, Jun 11, 2008 IP