Hi all.
I have my code below for a form I have. I was hoping for some advice/tips on how I would get the form on completion to re-direct to a success page and if there are errors to re-direct to an error page?
<?php
if(isset($_POST['submit'])) {
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
if(trim($_POST['phone']) == '') {
$hasError = true;
} else {
$phone = trim($_POST['phone']);
}
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
}
if(!isset($hasError)) {
$emailTo = '';
$body = "Name: $name \n\nEmail: $email \n\nPhone: $phone \n\nMessage:\n $message";
$headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Even if someone could list a few links to sites that they know of where I could learn this then that would be just as great.
Thanks
James
Page 1 of 1
Re-direct on success and error
#2
Posted 25 January 2012 - 07:08 PM
Use header requests - http://php.net/manua...tion.header.php
For example:
For example:
if(!isset($hasError)) {
$emailTo = '';
$body = "Name: $name \n\nEmail: $email \n\nPhone: $phone \n\nMessage:\n $message";
$headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
header('Location: /success-page/');
} else {
header('Location: /error-page/');
}
- ← User role inserted into comments section instead of User Name
- Server Side (PHP, Databases, ASP.NET, etc)
- Hi, Looking for help →
Share this topic:
Page 1 of 1
Help















