-4

I want the php contact to redirect to a thank you page after the user submitted correctly but it does not, all it says is this

Warning: Cannot modify header information - headers already sent by ()

this is my thanks.html

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Thank you we will get back to you </title>
<meta content="php, contact, form, thinking" name="keywords">
<meta content="Great success!" name="description">

<style>
p {
font-family: Cambria, Cochin, serif;
font-size: large;
margin-bottom: -5px;
}

h1 {
font-family: "Trebuchet MS", Arial, sans-serif;
font-size: xx-large;
color: #3399FF;
}
body {
padding: 10px;
background-color: #F4F4F4;
}
</style>

</head>

<body>
<h1>&nbsp;</h1>
<h1>Thank You!</h1>
<p>We've received your request,  we will get back to you soon.</p>

</body>

</html>

this is my php its called mailer.php

<?php 

$name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
    $from = 'From:you'; 
    $to = 'me@hotmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Number: $number\n Message:\n $message";


if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';

            header("Location: thanks.html");

        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }



}


 ?>

this is my contact.html page just the form part.

<h1 class="contactcat">Contact us for </h1>


   <form action="mailer.php"  method="post">

      <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Contact Number</label>
    <input name="number" type="text" placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <br/>

    <label>What's 2+2 (type only the number)</label>
         <input name="human" type="number" placeholder="Type Here">
        <br/>

    <input id="submit" name="submit" type="submit" value="Submit">

</form>
  • 2
    How is this different from your last question (http://stackoverflow.com/questions/18336364/my-web-hosting-company-says-there-is-something-wrong-with-php-code) which had the same problem? You're outputting content (echo) before sending a header. – j08691 Aug 20 '13 at 16:35
  • I do receive the email from this form – user2692442 Aug 20 '13 at 16:35
  • 1
    _No output allowed_ before `header()` or `setcookie()` or `session_start()`. Never, none, not even whitespace. – Michael Berkowski Aug 20 '13 at 16:35
  • I don't see the point in both redirecting the page and showing a message at the same time. If you redirect the user will never see the message anyway. – JJJ Aug 20 '13 at 16:37

1 Answers1

3
echo '<p>Your message has been sent!</p>';

You are echoing information out before trying to send the headers which will already be sent. You can't send anything to the browser before you try and send the headers.

Prisoner
  • 27,391
  • 11
  • 73
  • 102