I found a php mail form online that echos a text success message, but need to redirect it to a new page instead.

I love all the functions in the code except for the text echo.

I tried <input type="hidden" name="next-url" value="">

and <? header(Location: file.php); ?>

but the form redirect is ignored

and the header location gives me an error of not being able to do it because header already exists

Any ideas?

Here is the code.


<html>
<head>
<title>PHP mail Form</title>

<?php
// START NECESSARY MODIFICATIONS

// your name
$recipientname = "YOUR NAME";

// your email
$recipientemail = "YOU@YOURDOMAIN.COM";

// subject of the email sent to you
$subject = "Online-Form Response for $recipientname";

// send an autoresponse to the user?
$autoresponse = "yes";

// subject of autoresponse
$autosubject = "Thank you for your mail!";

// autoresponse message
$automessage = "This is an auto response to let you know that we've successfully received your email sent through our email form. Thanks! We'll get back to you shortly.";

// thankyou displayed after the user clicks "submit"
$thanks = "Thank you for contacting us.<br>We will get back to you as soon as possible.<br>";

// END NECESSARY MODIFICATIONS

?>

</head>
<body>

<?php
if($_POST) {

$Name = $HTTP_POST_VARS;
$Email = $HTTP_POST_VARS;
$Comments = $HTTP_POST_VARS;

// check required fields
$dcheck = explode(",",$require);
while(list($check) = each($dcheck)) {
if(!$$dcheck[$check]) {
$error .= "Missing $dcheck[$check]<br>";
}
}

// check email address
if ((!ereg(".+\@.+\..+", $Email)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $Email))){
$error .= "Invalid email address<br>";}

// display errors
if($error) {
?>
<b>Error</b><br>
<?php echo $error; ?><br>
<a href="#" onClick="(-1)">try again</a>
<?php
}
else
{

$browser = $HTTP_USER_AGENT;
$ip = $REMOTE_ADDR;

// format message
$message = "Online-Form Response for $recipientname:

Name: $Name
Email: $Email

Comments: $Comments

-----------------------------

Browser: $browser
User IP: $ip";

// send mail and print success message
mail($recipientemail,"$subject","$message","From: $Name <$Email>");

if($autoresponse == "yes") {
$autosubject = stripslashes($autosubject);
$automessage = stripslashes($automessage);
mail($Email,"$autosubject","$automessage","From: $recipientname <$recipientemail>");
}

echo "$thanks";
}
}
else {
?>
<form name="contactform" action="<?php echo $PHP_SELF; ?>" method="post">
<input type="hidden" name="require" value="Name,Email,Comments">
<br>
Name <br>
<input name="Name" size="25">
<br>
<br>
E-mail<br>
<input name="Email" size="25">
<br>
<br>
Comments<br>
<textarea name="Comments" rows="5" cols="35"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submitform">
<input type="reset" value="Reset" name="reset">
</p>
<br>
</form>

<?php } ?>
</body>
</html>

Dani AI

Generated

A short expert summary and practical fixes tied to the replies from the thread.

The form in 's first post prints a success message but needs to send the visitor to a different page. As pointed out, a server-side HTTP redirect is the cleanest solution when no output has been sent yet. 's client-side suggestion is a useful fallback when the script has already produced HTML. 's follow-up about delaying the redirect to show a confirmation for a second can be handled on the client side.

Why the "headers already sent" error happens and how to fix it

  • A redirect sent by the server will fail if any output has already been emitted: visible HTML, stray blank lines outside PHP tags, or a UTF-8 BOM at the start of the file.
  • Quick checklist: remove leading/trailing whitespace (especially before <?php), save files without a BOM, avoid closing ?> in pure-PHP files, and perform validation/mail sending before emitting HTML.
  • If relocating the redirect above output is not possible, process the POST in a dedicated handler script that only responds with a redirect; that avoids conflicts entirely.

Client-side options for a short confirmation then redirect

  • Meta refresh (one second pause):

    <meta http-equiv="refresh" content="1;url=/thankyou.html">
  • JavaScript delayed redirect (1 second):

    <script>
    setTimeout(function(){
      location.replace('/thankyou.html');
    }, 1000);
    </script>

Practical recommendations

  • Prefer the Post/Redirect/Get pattern: process the POST, then issue a server redirect so browsers don’t resubmit on refresh.
  • If headers keep failing, enable output buffering at the top (ob_start()), or refactor into a handler that issues the redirect before any HTML.
  • Modernize the handler (use $_POST, filter_var or filter_input for validation) to improve robustness and avoid deprecated features.

Recommended Answers

All 3 Replies

I believe that instead of echoing the thank you message, you can have it do a redirect instead.

Change this:

echo "$thanks";

To this:

header("Location: page_to_redirect_to.php");

You may get error message by sending header after the <head> tag. You may want to replace the redirection header with javascript below:


<script language="JavaScript" type="text/JavaScript">
<!--
window.location.href = "";
//-->
</script>


zippee

-------------------------

www.store2go.net

Thank you, zippee, this really helped.
But I don't know how to make it redirect after displaying the page for a second.

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.