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

HTML

Kayla Pasciullo
seal-mask
.a{fill-rule:evenodd;}techdegree
Kayla Pasciullo
Front End Web Development Techdegree Student 8,325 Points

PHP form error

Can anyone help me figure out why my PHP contact form keeps giving an error msg when "send message" is clicked?? This is my first website and first time using PHP so im a bit confused right now :)

to see it in action check out my contact page at www.kaylapasciullo.com

here are the php and html codes

<?php

// Email recipient
$EmailTo = "kayla.pasciullo@hotmail.com";

$errors = "";

// Name
if (empty($_POST["name"])) {
    $errors = "Name is required ";
} else {
    $name = $_POST["name"];
}

// Email
if (empty($_POST["email"])) {
    $errors .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// Subject
if (empty($_POST["subject"])) {
    $errors = "Subject is required ";
} else {
    $Subject = $_POST["subject"];
}

// Message
if (empty($_POST["message"])) {
    $errors .= "Message is required ";
} else {
    $message = $_POST["message"];
}

// Prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// Send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// Redirect to success page
if ($success && $errors == ""){
   echo 'success';
}
else{
    echo $errors;
}
?>

HTML 

 <form class="contact-form" id="contact-form" action="contact.php">
                    <h4 class="content-title">Message Me</h4>
                    <div class="row">
                      <div class="col-12 col-md-6 form-group"><input class="form-control" id="contact-name" type="text" name="name" placeholder="Name" required=""></div>
                      <div class="col-12 col-md-6 form-group"><input class="form-control" id="contact-email" type="email" name="email" placeholder="Email" required=""></div>
                      <div class="col-12 form-group"><input class="form-control" id="contact-subject" type="text" name="subject" placeholder="Subject" required=""></div>
                      <div class="col-12 form-group form-message"><textarea class="form-control" id="contact-message" name="message" placeholder="Message" rows="5" required=""></textarea></div>
                      <div class="col-12 form-submit"><button class="btn button-main button-scheme" id="contact-submit" type="submit">Send Message</button>
                        <p class="contact-feedback"></p>
                      </div>
                    </div>
                  </form>
'''

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Kayla Pasciullo! I would be interested to know what the php_error.log on the server is recording. I'd also like to know what version of PHP the server is using. We're not going to really be able to tell you more until we get some information about the errors that are being generated from PHP :smiley:

Looking forward to hearing back! :sparkles:

P.S. I'd also be interested to know if you plan to implement any CSRF checks and if not, why? :smiley:

Kayla Pasciullo
seal-mask
.a{fill-rule:evenodd;}techdegree
Kayla Pasciullo
Front End Web Development Techdegree Student 8,325 Points

seems like the error msg just says " main.js:354 " this is the first time I've used php... I am unable to find out which version I am running.... or not..?? yikes maybe this is part of the problem??

Thanks for informing my about CSRF checks... i will look into that. I am just starting out as a developer so I'm new to everything right now lol

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Kayla Pasciullo So one other thing I notice is that the form itself does not have a method attribute. For any of this to work it will need method="POST". Also make sure that contact.php exists. I can't tell what your files are called nor really anything about your server setup. I hate to say this, but I might avoid anything that requires the "back end" until you've reached the point in your studies where you can handle the server side stuff :smiley:

But don't worry. That's coming up! :sparkles: