Forum Moderators: coopster

Message Too Old, No Replies

Redirecting to thank you page

Help please!

         

silver500

1:08 pm on Mar 9, 2011 (gmt 0)

10+ Year Member



Hi Guys
Having some problem redirecting to thank you page after php email form

I am using the script below any help appreciated
Many thanks
Silver500
---------------------------

<?php
// modify the four lines below
$emailaddress = "me@mydomain.com";
$fromaddress = "website@mydomain.com";
$subject = "A Website Message";
$thankyouPage = "thanks.php"; // put in a url (or relative page) to go to a Thank You page
// modify the four lines above
putenv('TZ=EST5EDT'); // eastern time
session_start();
function processMail($inputArray) {
// validation
if (!preg_match("([a-zA-Z]+)",$inputArray['name'])) $errors .= "Please enter your name<br />";
if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i",$inputArray['email'])) $errors .= "Email address is not valid<br />";
if (!empty($inputArray[phone])) if (!preg_match("(^[-0-9\(\)\.]{7,}$)i",$inputArray[phone])) $errors .= "Phone number is not valid<br />";
if (!preg_match("([\w]+)",$inputArray['content'])) $errors .= "Your message is empty<br />";
// max messages
if (empty($errors)) $_SESSION['msgCount']++;
if ($_SESSION['msgCount'] >= "4") $errors .= "Messages have exceeded the maximum number allowed";
// stop if errors
if (!empty($errors)) { return "Your message was not sent<br />".$errors; }
// remove any input array variables not to be included in output email
unset($inputArray['send']);
// scrub variables
foreach ($inputArray as $key=>$value) {
$inputArray[$key] = trim($inputArray[$key]);
$inputArray[$key] = stripslashes($inputArray[$key]);
$inputArray[$key] = htmlspecialchars($inputArray[$key],ENT_QUOTES);
$inputArray[$key] = wordwrap($inputArray[$key],65,"<br />");
}
// need to use the config parms in this routine
global $subject,$emailaddress,$fromaddress,$thankyouPage;
// set headers
if (empty($fromaddress)) $fromaddress = $inputArray['email'];
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers .= "From: website<".$fromaddress.">\r\n";
// format message
$message = "<table cellpadding='5' border='1'>";
foreach ($inputArray as $key => $value) $message .="<tr><td><b>$key</b></td><td>$value</td></tr>";
$message .= "</table>";
// add source info to the message
$message .= "<br />Time of the message: ".date(" F d h:ia")."<br />";
$message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."<br />";
$message .= "Hostname: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."<br />";
// send message
if (!mail($emailaddress,$subject,$message,$headers)) return "There was a processing error<br />Your message was not sent";
if (!empty($thankyouPage)) { header('location: '.$thankyouPage); die(); }
global $_POST; unset($_POST);
return "Thank you<br />Your message has been sent";
}
if (!isset($_SESSION['msgCount'])) $_SESSION['msgCount'] = 0;
if (isset($_POST['send']) AND isset($_SESSION['msgCount'])) $message = processMail($_POST);
if (!empty($_POST)) {
foreach ($_POST as $key=>$value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars($_POST[$key],ENT_QUOTES);
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<div style="text-align: center; padding: 20px; width: 500px; margin: 50px auto; border: solid 1px black;">
<span style="font-weight: bold;">Contact Us</span><br><br>
your name<br><input type="text" style="width: 330px;" name="name" value="<?php echo $_POST['name']; ?>" maxlength="50"><br><br>
your email address<br><input type="text" style="width: 330px;" name="email" value="<?php echo $_POST['email']; ?>" maxlength="50"><br><br>
your phone number<br><input type="text" style="width: 330px;" name="phone" value="<?php echo $_POST['phone']; ?>" maxlength="50"><br><br>
your message<br><textarea name="content" style="resize: none; width: 330px; height: 100px;"><?php echo $_POST['content']; ?></textarea>
<br><br>
<input type="submit" name="send" value="submit">
<div style="margin-top: 10px; color: firebrick;"><?php echo $message; ?></div>
</div>
</form>
</body></html>

jatar_k

1:42 pm on Mar 9, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Silver500

you didn't mention what trouble you were having exactly but this part

if (!empty($thankyouPage)) { header('location: '.$thankyouPage); die(); }
global $_POST; unset($_POST);
return "Thank you<br />Your message has been sent";

the 2 lines after the redirect will never get executed so if you are relying on these to do something it will never get done.

Does it redirect? if not what exactly happens?

silver500

2:27 pm on Mar 9, 2011 (gmt 0)

10+ Year Member



Hi
I getting now the below message
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/fastplan/public_html/test2.php:12) in /home/fastplan/public_html/test2.php on line 134

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/fastplan/public_html/test2.php:12) in /home/fastplan/public_html/test2.php on line 134


And when I click submit I get the email but it going to the same page and not to the thank you page
Also get this message



Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/fastplan/public_html/test2.php:12) in /home/fastplan/public_html/test2.php on line 134

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/fastplan/public_html/test2.php:12) in /home/fastplan/public_html/test2.php on line 134

Warning: Cannot modify header information - headers already sent by (output started at /home/fastplan/public_html/test2.php:12) in/home/fastplan/public_html/test2.php on line 172

thank

jatar_k

6:29 pm on Mar 10, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



obviously there is output at those lines, as the error message states you can't have anything sent to the browser (no echos or blank lines, well ... nothing) before it creates the session cookie

you could try something as simple as moving your session_start() to the absolute first line of the file and ensure there are no blank lines or output before the <?php

you could try the header call explicitly to see if it works
header('location: thanks.php');

silver500

8:19 am on Mar 11, 2011 (gmt 0)

10+ Year Member



Hi
I have tried your suggestion –same result, I have checked the script on blank page and its working fine, maybe its a placement issue or compatibility with other php script on the page.
What do you think it’s the best way for me to approach it
Many thanks
Silver500