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) Enhancing a Form Wrapping It Up

coskun olcucu
coskun olcucu
5,340 Points

Have anyone done with extra credit question on Building Basic PHP website by enhancing the form section?

Hi, I already watched "Building basic webpage with PHP". I try to do extra credit assignment at the end of the course. Which require create an error_message array instead of variable and display all of the errors. here is my solution but it doesn't work as i want.

<?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));
    $category = trim(filter_input(INPUT_POST,"category",FILTER_SANITIZE_STRING));
    $title = trim(filter_input(INPUT_POST,"title",FILTER_SANITIZE_STRING));
    $format = trim(filter_input(INPUT_POST,"format",FILTER_SANITIZE_STRING));
    $genre = trim(filter_input(INPUT_POST,"genre",FILTER_SANITIZE_STRING));
    $year = trim(filter_input(INPUT_POST,"year",FILTER_SANITIZE_STRING));
    $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));
    $error_message=array(); //created an error message

 if($name==""  ){
        //the code below catch the case where the name is left blank entirely.
        //but if someone types just a space or a tab ????
        //it is sort of leaving a blank
        //we can use trim function
        $error_message[]= " fields:Name";

   }
  if($email=="") {
      $error_message[]= "fields: Email";//add message to the array

  }
  if($category=="") {
      $error_message[]= "fields: Category"; //add message to the array

  }
  if($title=="") {
      $error_message[]= "fields: title";

  }

   //Spam honey pot field for robot!!!
   if(!isset($error_message) && $_POST["address"]!=""){

      $error_message[]= "Bad form input";//add message to the array


   } 
   //the difference between require and include
   //what happens if they encounter an error
   //if you use include command and the file doesn't exist
   //PHP will throw a warning
   //but it will still execute the rest of the code
   //on the other hand,if you use require  and the file doesn't exist
   //then PHP will error and will not execute any more code
   //the other difference between them
   //how many times PHP will include the file
   //if you use include or require  on a same file  multiple times
   //it will load that file multiple times
   //this will work fine for HTML snippets 
   //like ourr header and footer filesize
   //But with other files like create new functions
   //this would cause an error
   //PHP mailer file is like that
   //we should only load it once
   //using require_once will ensure that nothing in that file
   //attempt to get loaded more than once
   //This solves the errors caused when redefining functions
   //as well as resetting variables that you may have changed
   //our code should be orginized in such a way
   //that you know you are only asking for the file once.
   //performence sligthly improved with require over require once
   //require once could be used her as well

   require("inc/phpmailer/class.phpmailer.php");
       $mail = new PHPMailer;

    if (!isset($error_message) &&!$mail->ValidateAddress($email)) {
      $error_message[]= "Invalid Email Address";//add message to the array

    }

    if(!isset($error_message)){
         $email_body = "";
         $email_body .= "Name " . $name . "\n";
         $email_body .= "Email " . $email . "\n";
         $email_body .= "Suggested Item\n";
         $email_body .= "Category " . $category . "\n";
         $email_body .= "Title " . $title . "\n";
         $email_body .= "Format " . $format . "\n";
         $email_body .= "Genre " . $genre . "\n";
         $email_body .= "Year " . $year . "\n";
         $email_body .= "Details " . $details . "\n";

       $mail->setFrom($email, $name);
       $mail->addAddress('coskunolcucu@hotmail.com', 'Coskun');     // Add a recipient

       $mail->isHTML(false);                                  // Set email format to HTML

       $mail->Subject = 'Personal Media Library Suggestion from ' . $name;
       $mail->Body    = $email_body;

      if($mail->send()) {
         header("location:suggest.php?status=thanks");
         exit();
       }
         $error_message[]= 'Message could not be sent.';
         $error_message[]='Mailer Error: ' . $mail->ErrorInfo;



    }   
 }




$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'll check out your suggestion shortly!</p>";

          } else {?>
             <?php
                if(isset($error_message)){
                   foreach($error_message as $error){

                              echo $error ."," ;
                   } 
                     echo " need to be filled out";
                 }

                else{

                 echo "<p>If you think there is something I'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(Required)</label></th>
                  <td><input type="text" id="name" name="name" value="<?php if(isset($name)){ echo $name; }   ?>" /></td>

                </tr>

                <tr>
                  <th> <label for="email">Email(Required)</label></th>
                  <td><input type="text" id="email" name="email" value="<?php if(isset($email)){ echo $email; }   ?> " /></td>

                </tr>
                 <tr>
                  <th> <label for="category">Category</label></th>
                  <td><select id="category" name="category"/>
                       <option value="">Select One</option> 
                        <option value="Books"  <?php if(isset($category) && $category=="Books"){ echo " selected "; } ?> >Book</option> 
                        <option value="Movies" <?php if(isset($category) && $category=="Movies"){ echo " selected "; } ?> >Movie</option> 
                         <option value="Music" <?php if(isset($category) && $category=="Music"){ echo " selected "; } ?>>Music</option> 

                  </select></td>

                </tr>
                <tr>
                  <th> <label for="title">Title</label></th>
                  <td><input type="text" id="title" name="title" value="<?php if(isset($title)){ echo $title; }  ?>" /></td>

                </tr>
                <tr>
                <th>
                    <label for="format">Format</label>
                </th>
                <td>
                    <select name="format" id="format">
                        <option value="">Select One</option>
                        <optgroup label="Books">
                            <option value="Audio"<?php
                            if (isset($format) && $format=="Audio") {
                                echo " selected";
                            } ?>>Audio</option>
                            <option value="Ebook"<?php
                            if (isset($format) && $format=="Ebook") {
                                echo " selected";
                            } ?>>Ebook</option>
                            <option value="Hardcover"<?php
                            if (isset($format) && $format=="Hardcover") {
                                echo " selected";
                            } ?>>Hardcover</option>
                            <option value="Paperback"<?php
                            if (isset($format) && $format=="Paperback") {
                                echo " selected";
                            } ?>>Paperback</option>
                        </optgroup>
                        <optgroup label="Movies">
                            <option value="Blu-ray"<?php
                            if (isset($format) && $format=="Blu-ray") {
                                echo " selected";
                            } ?>>Blu-ray</option>
                            <option value="DVD"<?php
                            if (isset($format) && $format=="DVD") {
                                echo " selected";
                            } ?>>DVD</option>
                            <option value="Streaming"<?php
                            if (isset($format) && $format=="Streaming") {
                                echo " selected";
                            } ?>>Streaming</option>
                            <option value="VHS"<?php
                            if (isset($format) && $format=="VHS") {
                                echo " selected";
                            } ?>>VHS</option>
                        </optgroup>
                        <optgroup label="Music">
                            <option value="Cassette"<?php
                            if (isset($format) && $format=="Cassette") {
                                echo " selected";
                            } ?>>Cassette</option>
                            <option value="CD"<?php
                            if (isset($format) && $format=="CD") {
                                echo " selected";
                            } ?>>CD</option>
                            <option value="MP3"<?php
                            if (isset($format) && $format=="MP3") {
                                echo " selected";
                            } ?>>MP3</option>
                            <option value="Vinyl"<?php
                            if (isset($format) && $format=="Vinyl") {
                                echo " selected";
                            } ?>>Vinyl</option>
                        </optgroup>
                    </select>
                </td>
            </tr>
            <tr>
                <th>
                    <label for="genre">Genre</label>
                </th>
                <td>
                    <select name="genre" id="genre">
                        <option value="">Select One</option>
                        <optgroup label="Books">
                            <option value="Action"<?php
                            if (isset($genre) && $genre=="Action") {
                                echo " selected";
                            } ?>>Action</option>
                            <option value="Adventure"<?php
                            if (isset($genre) && $genre=="Adventure") {
                                echo " selected";
                            } ?>>Adventure</option>
                            <option value="Comedy"<?php
                            if (isset($genre) && $genre=="Comedy") {
                                echo " selected";
                            } ?>>Comedy</option>
                            <option value="Fantasy"<?php
                            if (isset($genre) && $genre=="Fantasy") {
                                echo " selected";
                            } ?>>Fantasy</option>
                            <option value="Historical"<?php
                            if (isset($genre) && $genre=="Historical") {
                                echo " selected";
                            } ?>>Historical</option>
                            <option value="Historical Fiction"<?php
                            if (isset($genre) && $genre=="Historical Fiction") {
                                echo " selected";
                            } ?>>Historical Fiction</option>
                            <option value="Horror"<?php
                            if (isset($genre) && $genre=="Horror") {
                                echo " selected";
                            } ?>>Horror</option>
                            <option value="Magical Realism"<?php
                            if (isset($genre) && $genre=="Magical Realism") {
                                echo " selected";
                            } ?>>Magical Realism</option>
                            <option value="Mystery"<?php
                            if (isset($genre) && $genre=="Mystery") {
                                echo " selected";
                            } ?>>Mystery</option>
                            <option value="Paranoid"<?php
                            if (isset($genre) && $genre=="Paranoid") {
                                echo " selected";
                            } ?>>Paranoid</option>
                            <option value="Philosophical"<?php
                            if (isset($genre) && $genre=="Philosophical") {
                                echo " selected";
                            } ?>>Philosophical</option>
                            <option value="Political"<?php
                            if (isset($genre) && $genre=="Political") {
                                echo " selected";
                            } ?>>Political</option>
                            <option value="Romance"<?php
                            if (isset($genre) && $genre=="Romance") {
                                echo " selected";
                            } ?>>Romance</option>
                            <option value="Saga"<?php
                            if (isset($genre) && $genre=="Saga") {
                                echo " selected";
                            } ?>>Saga</option>
                            <option value="Satire"<?php
                            if (isset($genre) && $genre=="Satire") {
                                echo " selected";
                            } ?>>Satire</option>
                            <option value="Sci-Fi"<?php
                            if (isset($genre) && $genre=="Sci-Fi") {
                                echo " selected";
                            } ?>>Sci-Fi</option>
                            <option value="Tech"<?php
                            if (isset($genre) && $genre=="Tech") {
                                echo " selected";
                            } ?>>Tech</option>
                            <option value="Thriller"<?php
                            if (isset($genre) && $genre=="Thriller") {
                                echo " selected";
                            } ?>>Thriller</option>
                            <option value="Urban"<?php
                            if (isset($genre) && $genre=="Urban") {
                                echo " selected";
                            } ?>>Urban</option>
                        </optgroup>
                        <optgroup label="Movies">
                            <option value="Action"<?php
                            if (isset($genre) && $genre=="Action") {
                                echo " selected";
                            } ?>>Action</option>
                            <option value="Adventure"<?php
                            if (isset($genre) && $genre=="Adventure") {
                                echo " selected";
                            } ?>>Adventure</option>
                            <option value="Animation"<?php
                            if (isset($genre) && $genre=="Animation") {
                                echo " selected";
                            } ?>>Animation</option>
                            <option value="Biography"<?php
                            if (isset($genre) && $genre=="Biography") {
                                echo " selected";
                            } ?>>Biography</option>
                            <option value="Comedy"<?php
                            if (isset($genre) && $genre=="Comedy") {
                                echo " selected";
                            } ?>>Comedy</option>
                            <option value="Crime"<?php
                            if (isset($genre) && $genre=="Crime") {
                                echo " selected";
                            } ?>>Crime</option>
                            <option value="Documentary"<?php
                            if (isset($genre) && $genre=="Documentary") {
                                echo " selected";
                            } ?>>Documentary</option>
                            <option value="Drama"<?php
                            if (isset($genre) && $genre=="Drama") {
                                echo " selected";
                            } ?>>Drama</option>
                            <option value="Family"<?php
                            if (isset($genre) && $genre=="Family") {
                                echo " selected";
                            } ?>>Family</option>
                            <option value="Fantasy"<?php
                            if (isset($genre) && $genre=="Fantasy") {
                                echo " selected";
                            } ?>>Fantasy</option>
                            <option value="Film-Noir"<?php
                            if (isset($genre) && $genre=="Film-Noir") {
                                echo " selected";
                            } ?>>Film-Noir</option>
                            <option value="History"<?php
                            if (isset($genre) && $genre=="History") {
                                echo " selected";
                            } ?>>History</option>
                            <option value="Horror"<?php
                            if (isset($genre) && $genre=="Horror") {
                                echo " selected";
                            } ?>>Horror</option>
                            <option value="Musical"<?php
                            if (isset($genre) && $genre=="Musical") {
                                echo " selected";
                            } ?>>Musical</option>
                            <option value="Mystery"<?php
                            if (isset($genre) && $genre=="Mystery") {
                                echo " selected";
                            } ?>>Mystery</option>
                            <option value="Romance"<?php
                            if (isset($genre) && $genre=="Romance") {
                                echo " selected";
                            } ?>>Romance</option>
                            <option value="Sci-Fi"<?php
                            if (isset($genre) && $genre=="Sci-Fi") {
                                echo " selected";
                            } ?>>Sci-Fi</option>
                            <option value="Sport"<?php
                            if (isset($genre) && $genre=="Sport") {
                                echo " selected";
                            } ?>>Sport</option>
                            <option value="Thriller"<?php
                            if (isset($genre) && $genre=="Thriller") {
                                echo " selected";
                            } ?>>Thriller</option>
                            <option value="War"<?php
                            if (isset($genre) && $genre=="War") {
                                echo " selected";
                            } ?>>War</option>
                            <option value="Western"<?php
                            if (isset($genre) && $genre=="Western") {
                                echo " selected";
                            } ?>>Western</option>
                        </optgroup>
                        <optgroup label="Music">
                            <option value="Alternative"<?php
                            if (isset($genre) && $genre=="Alternative") {
                                echo " selected";
                            } ?>>Alternative</option>
                            <option value="Blues"<?php
                            if (isset($genre) && $genre=="Blues") {
                                echo " selected";
                            } ?>>Blues</option>
                            <option value="Classical"<?php
                            if (isset($genre) && $genre=="Classical") {
                                echo " selected";
                            } ?>>Classical</option>
                            <option value="Country"<?php
                            if (isset($genre) && $genre=="Country") {
                                echo " selected";
                            } ?>>Country</option>
                            <option value="Dance"<?php
                            if (isset($genre) && $genre=="Dance") {
                                echo " selected";
                            } ?>>Dance</option>
                            <option value="Easy Listening"<?php
                            if (isset($genre) && $genre=="Easy Listening") {
                                echo " selected";
                            } ?>>Easy Listening</option>
                            <option value="Electronic"<?php
                            if (isset($genre) && $genre=="Electronic") {
                                echo " selected";
                            } ?>>Electronic</option>
                            <option value="Folk"<?php
                            if (isset($genre) && $genre=="Folk") {
                                echo " selected";
                            } ?>>Folk</option>
                            <option value="Hip Hop/Rap"<?php
                            if (isset($genre) && $genre=="Hip Hop/Rap") {
                                echo " selected";
                            } ?>>Hip Hop/Rap</option>
                            <option value="Inspirational/Gospel"<?php
                            if (isset($genre) && $genre=="Inspirational/Gospel") {
                                echo " selected";
                            } ?>>Insirational/Gospel</option>
                            <option value="Jazz"<?php
                            if (isset($genre) && $genre=="Jazz") {
                                echo " selected";
                            } ?>>Jazz</option>
                            <option value="Latin"<?php
                            if (isset($genre) && $genre=="Latin") {
                                echo " selected";
                            } ?>>Latin</option>
                            <option value="New Age"<?php
                            if (isset($genre) && $genre=="New Age") {
                                echo " selected";
                            } ?>>New Age</option>
                            <option value="Opera"<?php
                            if (isset($genre) && $genre=="Opera") {
                                echo " selected";
                            } ?>>Opera</option>
                            <option value="Pop"<?php
                            if (isset($genre) && $genre=="Pop") {
                                echo " selected";
                            } ?>>Pop</option>
                            <option value="R&B/Soul"<?php
                            if (isset($genre) && $genre=="R&B/Soul") {
                                echo " selected";
                            } ?>>R&amp;B/Soul</option>
                            <option value="Reggae"<?php
                            if (isset($genre) && $genre=="Reggae") {
                                echo " selected";
                            } ?>>Reggae</option>
                            <option value="Rock"<?php
                            if (isset($genre) && $genre=="Rock") {
                                echo " selected";
                            } ?>>Rock</option>
                        </optgroup>
                    </select>
                </td>
            </tr>
              <tr>
                  <th> <label for="year">Year</label></th>
                  <td><input type="year" id="year" name="year" value="<?php if(isset($year)){ echo $year; }   ?>"/></td>

                </tr>

                <tr>
                  <th> <label for="details">Additional  Details</label></th>
                  <td><textarea id="details" name="details"><?php if(isset($details)){echo htmlspecialchars($_POST["details"]);}   ?></textarea></td>

                </tr>
                <tr>
                  <th> <label for="address">Address</label></th>
                  <td><input type="text" id="address" name="address"/>
                  <p>Please leave this field blank</p></td>

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

          </form>         
          <?php } ?>
     </div>
</div>

<?php include("inc/footer.php"); ?>
Simon Coates
Simon Coates
28,694 Points

(if anyone every reviews this, I made a couple notes. Probably doesn't cover everything.)

1) if using an array, you probably don't want to use isset.

<?php
$array = array();
var_dump(isset($array));//true
var_dump(empty($array));//true
var_dump(count($array));//0

2) if using an array, you need to ditch the !isset($error_message) tests. There were used to bypass lesser priority testing.

3) The above code splits the message for failing to send into two array entries. Should probably be one (and use concatenation), so each element is an individual error.