Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form SMTP with Google

Steven Schaner
Steven Schaner
14,675 Points

$name and $email are shown in my email but not $details. var_dump shows that $details has a stored string. Please help.

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>

2 Answers

Tim Makin
Tim Makin
17,261 Points

Hi Steven,

Try changing your if statement that checks the submission for empty values from:

if ($name == "" || $email == "" || $details = "") {

To this:

if ($name == "" || $email == "" || $details == "") {

The single equals will mean that $details is assigned the value of an empty string, overriding whatever was being contained in the variable hence the empty value in the email.

Hope this helps!

Tim

Steven Schaner
Steven Schaner
14,675 Points

Wow, thanks Tim. What a careless mistake. It helps to have someone else look at your code to catch minor mistakes. It works! Yay!

Austin Johnson
Austin Johnson
11,447 Points

Wow. I made an identical mistake in my if statement. Good catch!