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

Extra Credit: Re-Displaying the Submission

Does anyone have any ideas about how to implement the extra credit items on this exercise?

Extra Credit Instead of showing only the highest-priority error message, it might be nice to display all the error messages

Make the $error_message variable an array instead of a simple piece of text. Change the validation so that it runs all the checks, even after it encounters an error. Change the code that displays the error message to use a foreach loop that goes through all the elements in the error message array and display them.

1 Answer

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email =  trim($_POST["email"]);
    $message =  trim($_POST["message"]);
     $error_message = array();

    if($name == "" OR $email == "" OR $message == ""){
        $error_message[] = "Name, Email, and or Message is blank!";

    }
    {
    foreach($_POST as $value){
        if( stripos($value,'Content-Type')!== FALSE ){
            $error_message[] = "Error!";

        }
      }  
    }
        if($_POST["address"] != ""){
            $error_message[] = "Your not human";
        }

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

        if (!$mail->ValidateAddress($email)){
            $error_message[] = "Please enter a valid email!";

        }   
if(!isset($error_message)){
    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Message: " . $message;

    //Create a new PHPMailer instance
    $mail = new PHPMailer();
    //Set who the message is to be sent from
    $mail->SetFrom($email, $name);
    //Set who the message is to be sent to
    $address = "bdini.beard@gmail.com";
    $mail->AddAddress($address, 'Shirts 4 Mike');
    //Set the subject line
    $mail->Subject = 'Shirts 4 Mike Contact Forum |' . $name;
    //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
    $mail->MsgHTML($email_body);
    //Send the message, check for errors
    if($mail->Send()) {
     header("Location: contact.php?status=thanks");
     exit;
    }else{
        $error_message = "There was a problem sending the forum " . $mail->ErrorInfo;
        }
    }
}
?><?php 
$pageTitle = "Contact Mike";
$section = "contact";
$activeState = "contact";
include('inc/header.php'); ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>

                <?php

                    if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

                        <p>We will be in touch shortly</p>

                <?php } else { ?>


                <?php 
                 if(!isset($error_message)){
                    echo '<p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>';

                 }else{?><p class="message">
                     <?php 
                     foreach ($error_message as $error) {
                         echo $error;
                     }
                 } 

                  ?>
                    </p>

                <form method="post" action="contact.php">

                    <table>
                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name" value="<?php if(isset($name)){ echo htmlspecialchars($name);} ?>">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Email</label>
                            </th>
                            <td>
                                <input type="text" name="email" id="email" value="<?php if(isset($email)){echo htmlspecialchars($email);} ?>">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="message">Message</label>
                            </th>
                            <td>
                                <textarea name="message" id="message"><?php if(isset($message)){echo htmlspecialchars($message);} ?></textarea>
                            </td>
                        </tr>
                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>Humans please leave this empty or else.....</p>
                            </td>
                        </tr>                    
                    </table>
                    <input type="submit" value="Send">

                </form>
            <?php } ?>


        </div>

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