0

I'm attempting to redirect to Thank you! page after successfully validating form info and generating an email. But the header function does not work/ page does not redirect and I get my 404 instead. My code is given below:

<?php
/* Set e-mail recipient */
$myemail  = "myemail@myemail.com";
$subject = "Visitor Message";

/* Check all form inputs using check_input function */
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname = check_input($_POST['lastname'], "Enter your last name");
$email    = check_input($_POST['emailaddress']);
$message = check_input($_POST['message'], "Write your message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/*Prepare the message for the e-mail */
$emailmessage = "Hello!

Your have received a message!

Name: $firstname $lastname
E-mail: $email

Message:
$message

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $emailmessage);

/* Redirect visitor to the thank you page */
header('Location: http://www.mywebsite.com/thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="tsd_main.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>TSD</title>

</head>
 <body>
<div class="header">

<img src="twosundawnbanner.png" id="banner">

<ul>

<li><a href="index.hmtl">Home</a></li>

<!-- <li><a href="#news">News</a></li>
 --> 
<li><a href="#services">Services</a></li>

<li><a href="#projects">Projects</a></li>

<li><a href="#giving">Giving</a></li>

<li><a href="about.html">About</a></li>

<li><a href="#contact">Contact</a></li>

<li style="float:right"><a class="active" href="#shop">Shop</a></li>

</ul>

</div>
<div class="maincontent">
    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>
</div>
<div class="footer">
TSD &copy; 2017
</div>
</body>
</html>
<?php
exit();
}
?>
  • If you're receiving a 404 error, the redirect is working. Guessing there is no actual page at `http://www.mywebsite.com/thanks.html` that is being redirected to. Ensure the domain and location of the file are correct. Otherwise there is an `.htaccess` rule or other condition being applied to it, preventing the desired result. See: https://stackoverflow.com/a/768472/1144627 – Will B. Jul 20 '17 at 04:03
  • Thank you! You pointed me in the right direction! – Mikeal Vaughn Jul 22 '17 at 23:37

1 Answers1

-1

If you can just share the step by step screenshots will be better to understand and also try looking in the error log to understand real cause of error