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


Jacob Wick
Jacob Wick
8,912 Points

My code is executing the code in the "if" statement that checks for blank strings, even if the strings aren't blank!


//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.php';

  $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
  $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
  $details = trim(filter_input(INPUT_POST, "details", FILTER_SPECIAL_CHARS));

  if ($name == "" || $email == "" || $details == "") {
    echo "Please fill in required fields: Name, Email, and Details. ;)";
  if ($_POST["address"] != "") {
    echo "Bad form input.";
  if (!PHPMailer::validateAddress($email)) {
    echo "Invalid Email.";

  echo "<pre>";
  $email_body = "";
  $email_body .= "Name " . $name . "\n";
  $email_body .= "Email " . $email . "\n";
  $email_body .= "Details " . $details;
  echo $email_body;
  echo "</pre>";

  // Todo: Send Email
$pageTitle = "Suggest a Media Item";
$section = null;

include('inc/header.php'); ?>

<div class="section page">
  <div class="wrapper">
    <h1>Suggest a Media Item</h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
       echo "<p>Thanks for your e-mail! I&rsaquo;ll check out your suggestion shortly.</p>";
      } else { ?>
    <p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
    <form method="post" action="suggest.php">
          <th><label for="name">Name</label></th>
          <td><input type="text" id="name" name="name"></td>
            <th><label for="email">Email</label></th>
            <td><input type="email" id="email" name="email"></td>
          <th><label for="details">Details</label></th>
          <td><textarea name="details" id="details"></textarea></td>
        <tr style="display:none">
          <th><label for="address">Address</label></th>
          <td><input type="text" id="address" name="address"></td>
          <p>Please leave this field blank.</p>
      <input type="submit" value="Send">
    <?php } ?>
Jacob Wick
Jacob Wick
8,912 Points

I put in the extra var_dump(); so I could double-check what, if anything, was coming back blank. I'm getting the following array:

array(4) { ["name"]=> string(6) "Banana" ["email"]=> string(20) "banan@hongkong.paris" ["details"]=> string(6) "Humans" ["address"]=> string(0) "" }

So I know that nothing being checked for is coming back blank.

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Jacob Wick ! Looks like you're doing pretty well here, but one of them is returning an empty string. In the interest of helping you learn how to debug your code, I would suggest this. Before the if statement that you think is running erroneously, try doing a var_dump on $name, $email, and $details. One of those three will return an empty string :smiley:

I think this might get you headed in the right direction, but let me know if you're still stuck! :sparkles:

Jacob Wick
Jacob Wick
8,912 Points

Thanks! I finally figured out the error. That filter is really a mouthful!