Feeferlump Dogge
Feeferlump Dogge

Reputation: 3

How to change this form to redirect

I am using this PHP form below - how can I change it to redirect rather than opening a blank page that says "message sent"?

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',
$_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a
z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "[email protected]";
$email_subject = "New Message Help Hershey: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
    if ($key != 'subject' && $key != 'company') {
      if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS';
exit; }
    }
    $email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(@mail($your_email,$email_subject,$email_content)) {
    echo 'Message sent!'; 
} else {
    echo 'ERROR!';
}
}
?>

I don't know if it is a matter of just changing the echo 'Message Sent' line or how to change this so that it either redirects to a thank you page or back to the home page

Upvotes: 0

Views: 70

Answers (1)

Ewan
Ewan

Reputation: 15078

All you have to do is change your echo 'Message sent!'; line.

Try the following:

if(@mail($your_email,$email_subject,$email_content)) {
     header("Location: your_page.com/");
} else {
    echo 'ERROR!';
}

You can read more about it in the header page of the PHP manual.

Upvotes: 3

Related Questions