PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

Siraj Khan
Siraj Khan
3,451 Points

Whats wrong with my code in lesson:Using third-party library.? Please help.

Whats wrong with my code in lesson:Using third-party library.? Please help.

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

      if ($name == "" || $email == "" || $details == "") {
        echo "Please fill in the required fields: Name, Email and Details";
        exit;
      }


      if ($_POST["address" != ""]) {
        echo "Bad form input";
        exit;
      }


      if (!PHPMailer::validateAddress($email)) {
        echo "Invalid Email Address";
        exit;
      }


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


       // TO DO: Send email
       header("location:suggest.php?status=thanks");
}


$pageTitle = "Suggest a media Item";
$section = "suggest";

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 the email. I will check out your suggestion shortly!</p>";
    } else { ?>

    <p>If you think there is something I am 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" name="name" id="name"></td>
        </tr>
        <tr>
          <th><label for="email">Email</label></th>
          <td><input type="text" name="email" id="email"></td>
        </tr>
        <tr>
          <th><label for="details">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" name="address" id="address">
          <p>Please leave this field blank</p></td>
        </tr>

      </table>

      <input type="submit" value="Send"></input>

    </form>
  <?php } ?>

  </div>

</div>

<?php include('inc/footer.php'); ?>

1 Answer

Casey Phillips
Casey Phillips
5,452 Points

You are missing some semicolons here: ?

      $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))