Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a contact form using PHP mail() directly inside a page. It successfully emails te contact information to the recipient. However, it won't advance to the confirmation page identified in header() and outputs the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/redacted/redacted.com/wp-content/themes/diner/includes/theme-functions.php:752) in /home/redacted/redacted.com/wp-content/themes/diner/page.php on line 163

Any advice or resource links on how to fix this?

<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['contact_email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['contact_email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $inquirer_name = $_REQUEST['contact_name'] ;
    $email = $_REQUEST['contact_email'] ;
    wp_mail(
"redacted@gmail.com",
"Subject: Test contact",
"Name: $inquirer_name
Email: $email "
);
header("Location:http://redacted.com/contact-confirmed");
    }
  } else { //if "email" is not filled out, display the form ?>

<form method="post" action="http://redacted.com/contact">
<ul>
<li>First Name: <input name="contact_name" type="text" /></li>
<li>Email: <input name="contact_email" type="text" /></li>
<li><input type='submit' /></li>
</ul>

<?php } ?>
share|improve this question
    
What happens when you are using wp_mail(). There is no excuse to use plain mail(). ;) –  toscho Nov 20 '12 at 2:06
    
Ah, will do! thanks! –  Marc P Nov 20 '12 at 16:11

1 Answer 1

You can't use header in the middle of a page, content has already been sent to the browser, so that means headers were already sent.

Look at the code in the myriad of form plugins out there to see how they handle this- hook an action before the template is loaded and do your form processing and checking there.

share|improve this answer
    
Ah, I see: hence that specific error. I will look into it, thanks! –  Marc P Nov 20 '12 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.