How do I make my php contact form redirect the user to a thank you page after sending the email.

?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['fname'] ;
  $message = $_REQUEST['message'];
  mail("info@website.com", "$subject",
  $message, "From:" . $email);
  echo "<a href='http://www.website.com'>Thank you for your email, we will get back to you in the next 24 hours, click to go to the Home page</a>"; 
  }
?>

the html

<form id="contact" action="email.php" method="post">
            <div class="form_settings">
              <p><span>Name</span><input class="contact" type="text" id="fname" name="fname"/></p>
              <p><span>Email Address</span><input class="contact" type="text" id="email" name="email"/></p>
              <p><span>Message</span><textarea class="contact textarea" rows="5" cols="50" id="message" name="message"></textarea></p>
              <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
            </div>
          </form>

Dani AI

Generated

is right to use a redirect after the mail call; two gotchas often trip people up: 1) call header('Location: ...') before any output (no echoes, no whitespace, no BOM) and 2) always exit right after the redirect. Also prefer $_POST over $_REQUEST, validate inputs, and use the Post/Redirect/Get pattern so a browser refresh does not resubmit the form.

<?php
// email.php (place this at the very top of the file; no output above)
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: /contact.html', true, 303);
    exit;
}

$name    = trim((string)($_POST['fname'] ?? ''));
$email   = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$message = trim((string)($_POST['message'] ?? ''));

if (!$email || $name === '' || $message === '') {
    header('Location: /error.html', true, 303);
    exit;
}

// prevent header injection in text fields
$name = preg_replace('/[\r\n]+/', ' ', $name);

// use your domain in From for deliverability; put the user in Reply-To
$headers = "From: no-reply@website.com\r\n"
         . "Reply-To: {$email}\r\n"
         . "X-Mailer: PHP/" . phpversion();

$subject = "Contact form from {$name}";
$ok = mail('info@website.com', $subject, $message, $headers);

// redirect based on result
header('Location: ' . ($ok ? '/thanks.html' : '/error.html'), true, 303);
exit;

A few extra tips:

  • Do not put the user-supplied email directly in the From header; use Reply-To instead to avoid SPF/DMARC issues. See the mail() header examples for details.
  • Validate with filter_var() (e.g., FILTER_VALIDATE_EMAIL) rather than regexes.
  • If you need attachments, SMTP auth, or better error messages, switch to a library such as PHPMailer rather than raw mail(). Helpful references: PHP mail() manual, filter_var() validation examples, and PHPMailer on GitHub.

Hi, at line 9 of the email.php script do:

// success page
if(mail(...))
    header('Location: ');

// error page
else
    header('Location: ');

// stop script execution
exit;

And remove the echo statement at line 11. Docs:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.