PHP Header error

Hi,
I am not a coder- I have some PHP for a contact web site page that works and when i copied it to a new site, it errors on header as follows

Warning: Cannot modify header information - headers already sent by (output started at /home/content/79/7955179/html/contact.php:5) in /home/content/79/7955179/html/contact.php on line 35

Here is the code and the error moves with the header statement

<?php $page_title = 'PurrfectPCs.'; if (isset($_POST['submitted'])) { $errors = array(); if (empty($_POST['name'])) { $errors[] = 'Fill in your name.'; } if (empty($_POST['compname'])) { $errors[] = 'Fill in your company name.'; } if (empty($_POST['subject'])) { $errors[] = 'Fill in the subject.'; } if (empty($_POST['email'])) { $errors[] = 'Fill in your e-mail address.'; } if (empty($_POST['body'])) { $errors[] = 'Fill in the body of the e-mail.'; } if (empty($errors)) { $body = "Name: {$_POST['name']}\nCompany Name: {$_POST['compname']}\nE-mail: {$_POST['email']}\nBody: {$_POST['body']}\n"; mail ('[email protected]', $_POST['subject'], $body); $URL="thankyou.htm"; header ("Location: $URL"); }else{ echo "

Error!

\nThe following errors have occurred:
"; foreach ($errors as $msg) { echo "
  • $msg
    \n
  • "; } echo "
    Please go back and fill in the required information.
    "; } }else{ ?>

    Any help is appreciated

    Is this a stand alone page, or do you have html in it?

    I have html in it

    Headers must be executed before any html is displayed. ( As far as I’m aware )
    I don’t know if simply putting your code in the top of your page before all other code will work. But its worth a shot.
    And see how this goes.
    [php]$URL=“thankyou.htm”;
    header (“Location: $URL”);
    die();[/php]

    Did not work- same error

    all i have above the PHP is this

    I deleted that and it still errored out

    i tried the die(); too- same error

    I know lothop hate to use this :smiley: but try changing out header location $url to:
    [php]
    echo"<meta http-equiv=refresh content=0;url=thankyou.htm>";
    [/php]
    :wink:

    No error but the page stalls and does not go to the thankyou .htm page

    Ok I centered the send buttons and the form and now it will not send the email.

    Question- is there any templates for html contact forms that will send email so i can toss PHP?

    Hi there,

    Just want to point out to those who aren’t aware, ANY whitespace or anything at all before the PHP tag (or any outputting commands within the PHP) will send headers. Make sure your <?php tag is at the very beginning of the file and that there are no ‘print’ or ‘echo’ functions being called.

    If you want, you can try using my custom written redirect function:
    [php]/**

    • Redirects to the page specified in $location
    • @param string $location Where to redirect to
    • @param integer $delay Delay in seconds
      /
      function redirect($location,$delay=0)
      {
      if(headers_sent() === true && $delay <= 0)
      {
      header(“Location: “.$location);
      }
      else
      {
      echo $delay == 0 ? ‘’
      : ‘’;
      echo ‘’;
      }
      }[/php]

    if it is stalling and not transferring that means the script is most likely dieing at the mail send, some host servers require that you add a FROM section to the mail scripts, like I use bluehost and it has to be
    mail ($to, $subject, $message, $from); and the $from part needs to be an actual email account that you have with them. try adding that in there and see if that helps, also just to be sure put the $_POST[‘subject’] in to the variable:
    $subject="{$_POST[‘subject’]}";
    Just before sending mail.

    You know me too well ::slight_smile:

    delaneybob - It seems like you’ve solved your header problem, follow plintu’s advice and you should be sorted.

    Ok I am trying some of the suggestions- please bear with me

    Ok here is where I am at

    I removed all the stuff before the php statement and the script worked and went to the thank you and was redirected to the home page- great. I did not add the from statement

    2 issues:

    1. Email takes like 20 minutes to show up
    2. the Contact page formatting is whacked. Since it is a PHP page it does not appear that i can make it formatted the same as the other HTML pages. Expression does not present the site as posted

    When i mean whacked- i mean I want the page to be justified center, I am looking at the code and trying to make the page the same and can nt see a way to do it

    I have it almost there ;D

    The table with Home and testimonials is off on the PHP page in size but looks fine in Expression- any suggestions?

    http://www.purrfectpcs.com/contact.php

    I would say give the form a background like the image above it, other than that thumbs up glad you got it working!

    The menu changes size when in contact us.

    http://www.purrfectpcs.com/index.htm looks good
    http://www.purrfectpcs.com/testimonials.htm looks good
    http://www.purrfectpcs.com/contact.php size changes and isn’t uniform.

    My other suggestion is not to have the page scrolling. Have to scroll down slightly to view the form buttons.

    I know it might seem kinda nit picky, but maybe to create more space remove the margin from the top of the page. Also you could slightly trim the main image a bit just to nudge it up.

    Other suggestion is to have a border for the sides. just a thin line assigned to the background image, only needs to be 2 pixels high. And stroke (add thin border) the main image.

    Hi,
    I messed with the table a long while and have it good to go for now.

    Much appreciated. If there is a donation site for the forum please forward

    Sponsor our Newsletter | Privacy Policy | Terms of Service