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 form validation issues

Having issues with form validation. Got the form to send an email and return to the page with error/success messages but it continued to send the email even when the message field was empty. So i added if($message == "") { header("Location: contact.php?status=error"); exit; } But now the whole thing doesn't work. its very annoying. Here's the code in its entirety: <?php if($_SERVER['REQUEST_METHOD']=="POST"){

$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            echo "There was a problem with the information you entered.#3v1lSp4m80t";    
            exit;
        }
    }
        if ($_POST["address"] != "") {
        echo "Your form submission has an error. #h0n3yp0t7r4p  ";
        exit;
    }
        if($message == "") {
        header("Location: contact.php?status=error");
        exit;
    }


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


$email_body = "";
$email_body = $email_body . "Name:" . $name . "<br>";
$email_body = $email_body . "Email:" . $email . "<br>";
$email_body = $email_body . "Message:" . $message;

$mail->SetFrom($email, $name);
    $address = "david@nothing-nowhere.co.uk";
    $mail->AddAddress($address, "NothingNowhere");
    $mail->Subject    = "NothingNowhere Contact Form Submission | " . $name;
    $mail->MsgHTML($email_body);

    if(!$mail->Send()) {
    header("Location: contact.php?status=emailError");
    exit;
    }


header("Location: contact.php?status=thanks");
exit;
}
}
?>

Thanks David

What kind of error do you get?

1 Answer

When the form is submitted it process but returns a white screen. I have put a temporary fix in place (making the form field required) which seems to solve the problem. Its just annoying it won't work in php