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) Adding a Basic Form Working with Post Variables

Steven Schaner
Steven Schaner
14,675 Points

$name, $email are shown in email but not $details. var_dump shows that $details has a stored string. I'm confused.

My email submission to gmail works correctly but leaves out the string that's stored as $details. I ran a var_dump and saw that $details had a string stored into it but $email_body looked like Name: steve Email: sch______@gmail.com Details:

I don't know why everything is shown except $details. I'm not sure what code to post that would help someone answer this question but here is some. Thank you for any help.

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_SANITIZE_SPECIAL_CHARS));

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

  $email_body = "";
  $email_body .= "Name: " . $name . "\n";
  $email_body .= "Email: " . $email . "\n";
  $email_body .= "Details: " . $details . "\n";

Here's my form:

<form method="post" action="suggest.php">
      <table>
        <tr>
          <th><label for="name">Name:</label></th>
          <td><input type="text" id="name" name="name" value=""></td>
        </tr>
        <tr>
          <th><label for="email">Email:</label></th>
          <td><input type="text" id="email" name="email" value=""></td>
        </tr>
        <tr>
          <th><label for="name">Suggest Item 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" value=""></td>
          <p>Please leave this field blank.</p>
        </tr>
      </table>
      <input type="submit" value="Send">
    </form>
Steven Schaner
Steven Schaner
14,675 Points

Posted this question at the wrong section of the course. Oops. This should be posted at the last section of this course.

1 Answer

There is a typo in the second if statement where you actually set $details to an empty string: $details = ""