0

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(
"[email protected]",
"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 } ?>
1
  • What happens when you are using wp_mail(). There is no excuse to use plain mail(). ;) Commented Nov 20, 2012 at 2:06

1 Answer 1

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.

1
  • Ah, I see: hence that specific error. I will look into it, thanks! Commented Nov 20, 2012 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.