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

PHP Validation $email

I've followed this video through, and get the error when sending an invalid email, but I also get the same happen when I pass through a valid email too.

I did a quick check, and a snippet of my code is below, and I tried to echo $email, which works fine until after the check to see if it is blank or not, at which point echo $email; returns nothing, and I don't see why that would happen?

<?php

// POST Method for sending Form information
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));

    //echo $email;

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

    echo $email;

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

    require("inc/phpmailer/class.phpmailer.php");

    $mail = new PHPMailer;


    if (!$mail->ValidateAddress($email)) {
        echo "E-Mail:" . $email;
        echo "Invalid Email Address";
        exit;
    }

    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .=  "Email " . $email . "\n";
    $email_body .=  "Details " . $details . "\n";

    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.com', 'Joe User');
    $mail->addAddress('from@example.com');
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->setCC('cc@example.com');
    $mail->setBCC('bcc@example.com');

    $mail->addAttachment('/var/tmp/file.tar.gz');
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');
    $mail->isHTML(true);

    $mail->Subject = 'Here is the subject';
    $mail->Body = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';



    header("location:suggest.php?status=thanks");
}


$pageTitle = "Suggest a Media Item";
$section = "suggest";
include("inc/header.php"); 

?>

1 Answer

Double "=" required on the IF statement. Rookie mistake.