Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Enhancing a Form Re-Displaying the Submission

Header function : header() for displaying thanks after form submission not working!!!!!

Hello everyone

I have created the php form after viewing the course. The form submission works fine but it doesn't take the senders to the thanks message.

Please help me to fix the bug. Thanks in advance.

Here is my code:

<?php $pageTitle = "Your Feedback is very important to us"; ?>

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title><?php echo $pageTitle; ?></title> <link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet"> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/style1.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body>

<?php 

if($_SERVER ["REQUEST_METHOD"] == "POST"){ $name = trim (filter_input(INPUT_POST,"name", FILTER_SANITIZE_STRING)); $email = trim (filter_input(INPUT_POST,"email", FILTER_SANITIZE_STRING)); $comments = trim (filter_input(INPUT_POST,"comments", FILTER_SANITIZE_SPECIAL_CHARS));

if($name == "" || $email == "" || $comments == "" ){ $error_message = "Please fill in the required fields: Name,Email and Comments";

} if(!isset($error_message) && $_POST["address"] != ""){ $error_message = "Bad form input";

}

require ("phpmailer/class.phpmailer.php"); $mail = new PHPMailer;

if(!isset($error_message) && !$mail ->ValidateAddress($email)){ $error_message = "Invalid Email Address";

} if (!isset($error_message)){

   $email_body = "";
   $email_body .= "Name " . $name . "\n";
   $email_body .= "Email " . $email . "\n";
   $email_body .= "Comments " . $comments . "\n";


  $mail->setFrom($email, $name);
  $mail->addAddress('my email address', 'Hasina Akhter');     // Add a recipient

  $mail->isHTML(false);                                  // Set email format to HTML

  $mail->Subject = 'Feedback from '. $name;
  $mail->Body    = $email_body;
  if($mail->send()) {
     header("location:feedback.php?status=thanks");
  }
   $error_message = 'Message could not be sent.';
   $error_message .= 'Mailer Error: ' . $mail->ErrorInfo; 

}

}

$pageTitle = "Your Feedback is very important to us"; $section = "feedback"; ?> <div class="section page"> <div class="container"> <h2>Your Feedback is very important to us!</h2> <p>Give us your feedback by completing the form below:</p>

    <?php if (isset($_GET["status"]) && ($_GET["status"]) == "thanks"){

echo "<p>Thanks for your feedback. I’ll get back to you shortly.</p>";

} else { if(isset($error_message)){ echo "<p class='message'>" .$error_message . "</p>"; } else{ "<p>Give us your feedback. Complete the form below:</p>"; } ?>

 <form method="post" action="feedback.php">
   <table>
   <tr>
     <th><label for="name">Name (required)</label></th>
     <td> <input type="text" id="name" name="name" value="<?php if(isset($name)){echo $name;} ?>"/></td>
   </tr>

    <tr>
     <th><label for="email">Email  (required)</label></th>
     <td> <input type="text" id="email" name="email" value="<?php if(isset($email)){echo $email;} ?>"/></td>
   </tr>

   <tr>
     <th><label for="comments">Comments</label></th>
       <td> <textarea name="comments" id="comments"><?php if(isset($comments)){ echo htmlspecialchars($_POST["comments"]); } ?></textarea></td>
   </tr>
     <tr style=display:none;>
     <th><label for="address">Address</label></th>
     <td> <input type="text" id="address" name="address"/>
    <p>Please leave this place blank</p></td>
   </tr>

</table>
<input type="submit" value="send"/>

   </form>
   <?php }?>
 </div>

</div>

</body>

</html>

1 Answer

Dennis Amiel Domingo
Dennis Amiel Domingo
16,213 Points

Put this code

ob_start();

right before your $email_body variable.

Like this:

} if (!isset($error_message)){
   ob_start();
   $email_body = "";
   $email_body .= "Name " . $name . "\n";
   $email_body .= "Email " . $email . "\n";
   $email_body .= "Comments " . $comments . "\n";

What's happening is that other html elements above your header code are outputting first before your header function. Using the ob_start() function stores all outputs in an internal buffer and lets your header function output as normal.