zero-perfoliate
zero-perfoliate

Author Topic: PHP Email Submit Redirect Code Problem  (Read 303 times)

Offline jenbennett

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
PHP Email Submit Redirect Code Problem
« on: May 11, 2010, 10:10:38 AM »
Hi all!

Created a form that will be submitted via email and the PHP code that was used to create the submission is generating "Mail Sent" on the page before the form is even filled out. We decided to change the code to redirect to a thank you page rather than simply displaying "Mail Sent" or "Mail send failure" when submitted. However, the code I have tried is giving me errors on the page or just not redirecting.

The following is the original code that is displaying incorrectly. Is there a simple fix to the code that would redirect properly? We would like to redirect to a thank you page when successfully submitted and an error page when failed.

Code: [Select]
        <?php

[i]i deleted out all of the unnecessary part of the code that links to the form fields to save reading time[/i]

/*Sending Email*/

$to "me@domain.com";
$subject "New Application";
$message "[i]also deleted all of this unneccessary info to save time[/i]"

if(mail($to$subject$message"From: $from"))
echo 
"Mail sent";
else
echo 
"Mail send failure - message not sent";
?>


I tried replacing

Code: [Select]
echo "Mail sent";
else
echo "Mail send failure - message not sent";

with

Code: [Select]
echo $mail_sent ? header("Location: thankyou.php"): "Mail failed";

but that just results in "Mail failed" showing on the page instead of "Mail sent" even without anything being filled out.

Also tried

Code: [Select]
if($mail_sent) {
header('Location: thankyou.php');
exit();
}else{
echo 'Mail failed';
}

but no luck there either.

Thank you for any help in advance!   :)

Offline Sergey Popov

  • PHP Helpers
  • ***
  • Posts: 31
  • Karma: +0/-0
    • Freelance PHP Developer
Re: PHP Email Submit Redirect Code Problem
« Reply #1 on: May 13, 2010, 02:48:15 AM »
Hi jenbennett,

Your second version looks fine:

Code: [Select]
if($mail_sent) {
  header('Location: thankyou.php');
  exit;
}else{
  echo 'Mail failed';
}

Are you getting error message saying "...headers already sent.." ?
You may need to check there are no any output before you start sending headers.
Any output of space, etc. above the header() function call will result in error message.
Refer to php documentation or PHP Help forum for more info and for answers to your questions