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 Simple PHP Application Wrapping Up The Project Using A Third-Party Library

Connor Glynn
Connor Glynn
6,381 Points

Nothing changes after I add the phpmailer code, any ideas?

I've added the code from the video and it doesn't seem to validate the email address. It still checks to see if their is one there (from previous videos). I can't seem to see an issue, maybe an update phpmailer? Thank you in advance!

<?php
//checks to see if the form has been submitted below
If ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);
    $reason = trim($_POST["reason"]);

    if ($name == "" || $email == "" || $message == "") {
        echo "You must enter a value for name, email address and message.";
        exit;
    }

    foreach($_POST as $value) {
        if(stripos($value,'Content-Type:') !== FALSE) {
            echo "There was a problem with the information you entered.";
            exit;
        }
    }

    if($_POST["address"] != "") {
        echo "Your from submission has an error.";
        exit;
    }

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

    if (!$mail->ValidateAddress($email)) {
        echo "You must specify a valid email address.";
        exit;
    }

    //stores the form data in a variable
    $email_body =  "";
    $email_body = $email_body . "Name: " . $name . "\n";
    $email_body = $email_body . "Email: " . $email . "\n";
    $email_body = $email_body . "Message: " . $message . "\n";
    $email_body = $email_body . "Reason: " . $reason . "\n";

    //TODO: Send email

    header("Location: contact.php?status=thanks");
    exit; 
}
//declares variables to be used in header.php
$pageTitle = "Contact Mike"; 
$page = "contact";

//includes header.php
include('inc/header.php'); 
?>

<!-- Start of Contact Page -->
    <div class="section page">

        <div class="wrapper">
            <h1>Contact</h1>

            <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { ?>

                <p>Thanks for the email! I'll be in touch shortly!</p>

            <?php } else { ?>                   

                <p>I'd love to hear from you! Complete the form to send me an email!</p>

                <form method="post" action="contact.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="message">Message:</label>
                            </th>
                            <td>
                                <textarea name="message" id="message"></textarea>
                            </td>   
                        </tr>       

                        <tr>
                            <th>
                                <label for="reason">Reason For Inquiry</label>
                            </th>
                            <td>
                                <select id="reason">
                                    <option>Fault</option>
                                    <option>Query</option>
                                    <option>Other</option>
                                </select>
                        </tr>

                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <textarea id="address" name="address"></textarea>
                                <p> **Not to be filled out** </p>
                        </tr>
                    </table>
                    <input type="submit" value="submit">
                </form>
            <?php } ?>
        </div><!-- End wrapper -->

    </div> ><!-- End section page -->
<!-- End of Contact Page -->

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

1 Answer

Connor Glynn
Connor Glynn
6,381 Points

I found an answer to this.

For some reason it works when using 'require' instead of 'require_once'. This may be due to an update in PHP so this may not have been clearly explained in the video?

Either way, if anyone would like to elaborate on this fix as to why it works I would more than happy to hear an explanation :)

Cristian Altin
Cristian Altin
12,165 Points

From PHP documentation: The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

It should be the same unless you changed something in the path too.

The official documentation for PHPMailer suggest to use: require 'PHPMailerAutoload.php';

Connor Glynn
Connor Glynn
6,381 Points

Hey Cristian,

My code seems to be working now regardless of what I put in xD

I just copied the code i gave and it works!

I'm starting to see the struggles as a developer xD

Thanks!

Cristian Altin
Cristian Altin
12,165 Points

Welcome to our world hehe. But it's fun, else you wouldn't be here anyway. Am I correct? ;-)