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

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!

<?php 

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.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_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. ;)";
    var_dump($_POST);
    exit;
  }
  if ($_POST["address"] != "") {
    echo "Bad form input.";
    exit;
  }
  if (!PHPMailer::validateAddress($email)) {
    echo "Invalid Email.";
    exit;
  }

  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
  header("location:suggest.php?status=thanks");
}
$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">
      <table>
        <tr>
          <th><label for="name">Name</label></th>
          <td><input type="text" id="name" name="name"></td>
        </tr>
        <tr>
            <th><label for="email">Email</label></th>
            <td><input type="email" id="email" name="email"></td>
        </tr>
        <tr>
          <th><label for="details">Details</label></th>
          <td><textarea name="details" id="details"></textarea></td>
        </tr> 
        <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>
        </tr>      
       </table>
      <input type="submit" value="Send">
    </form>
    <?php } ?>
  </div>
</div>
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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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!