I’ve searched everywhere to find a solution, but I don’t have found it.
I’ve a form on my website, who work perfectly on localhost, but have some troubles for working well when hosted on my website server. When the user clicks on submit, if the fields are perfectly completed the email is send but there are no redirection to the link mentionned on the header(‘Location:’). In place to go to the page "thanks.php", the page "contact.php" is like reloaded in an empty version (full white screen).
Can you please help me to find a solution?
Thanks a lot!
PHP code before
<?PHP
// form handler
function validateFeedbackForm($arr)
{
extract($arr);
if(!isset($name, $email, $subject, $message)) return;
if(!$name) {
return "DON'T PANIC! It seems, there are an error with the name.";
}
if(!preg_match("/^S+@S+$/", $email)) {
return "DON'T PANIC! It seems, there are an error with the email address.";
}
if(!$subject) $subject = "Contact from website";
if(!$message) {
return "DON'T PANIC! It seems, there are an error with the message.";
}
// send email and redirect
$to = "[email protected]";
$headers = $subject. " - " .$name. " " .$email. "rn" .
'Reply-To: ' .$email . "rn" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location: ./thanks.php');
exit;
}
// execution starts here
if(isset($_POST['sendfeedback'])) {
// call form handler
$errorMsg = validateFeedbackForm($_POST);
}
?>
Form code
<form method="POST" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" accept-charset="UTF-8">
<?PHP
if(isset($errorMsg) && $errorMsg) {
echo "<p id="error-msg">",htmlspecialchars($errorMsg),"</p>nn";
}
?>
<input placeholder="Your name *" class="input-class ct-yourname" type="text" size="48" name="name" value="<?PHP if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>">
<input placeholder="Your email address *" class="input-class ct-email" type="email" size="48" name="email" value="<?PHP if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>">
<input placeholder="Subject of your message" class="input-class ct-subject" type="text" size="48" name="subject" value="<?PHP if(isset($_POST['subject'])) echo htmlspecialchars($_POST['subject']); ?>">
<textarea placeholder="Hey, ... *" class="input-message ct-message" name="message" cols="48" rows="8"><?PHP if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']); ?></textarea>
<input class="btn-submit" type="submit" name="sendfeedback" value="Send Message">
</form>
2
Answers
Thanks to OMi Shah to put me on the good way to find a solution. Nothing was generated for me before the
header()
when I looked at my code... but in fact it wasn't totally true.I get a little piece of code just before the PHP code block :
<!DOCTYPE php>
. This line was just the reason of the none redirection (a loop on the same page in fact). If some other beginner got the same problem, don't forget to put no code before your PHP code.Thanks & take care!
Possible reasons:
Some output is getting generated before the
header()
is called which is causing the problem. Make sure no output is generated before theheader()
is called.Make sure the file in which your form is included is
utf-8
encoded, not other.run
phpinfo()
and make sure thatoutput_buffering
isnot empty
Solution: You can add
ob_start();
after the PHP opening tag<?php
in your script as: