Web Design Forum: Re-direct on success and error - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Re-direct on success and error Rate Topic: -----

#1 User is offline   jame5payne 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 48
  • Joined: 02-December 11
  • Reputation: 0

Posted 25 January 2012 - 05:06 PM

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
0

#2 User is online   Spitfire 

  • Mighty Pirate™
  • PipPipPipPip
  • Group: Members
  • Posts: 891
  • Joined: 05-February 11
  • Reputation: 189
  • Gender:Male
  • Location:Berkshire
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 25 January 2012 - 07:08 PM

Use header requests - http://php.net/manua...tion.header.php

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/');
}

0

#3 User is offline   jame5payne 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 48
  • Joined: 02-December 11
  • Reputation: 0

Posted 26 January 2012 - 10:22 AM

Thanks for the tips/advice spitfire :good:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users