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 trialMax Carlquist
2,791 PointsPHP contact form not working because of http_response_code() in mailer.php
I've used the tutorial at http://blog.teamtreehouse.com/create-ajax-contact-form
However, I still get this message in my alert above the contact form: Fatal error: Call to undefined function http_response_code() in (site details)mailer.php on line 17. This only happens if I keep the http_response_code();
However,
If i remove it I get the the alert "Ooops Something went wrong..." I'm definetly out in space when it comes to php and some javascript. Any help would be great!
5 Answers
Christopher Borchert
14,814 PointsAlso, for testing purposes, change the "Oops!" messages to be more descriptive: The first error is triggered when a field is missing or isn't validated, so I'd change
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
to
//Disabled for now :)
//http_response_code(400);
echo "Oops! Missing field or validation failed.";
The second oops is triggered by a return value of "false" from the mail function. Change that section of code to
//Disabled for now :)
//http_response_code(500);
echo "Oops! Mail() is probably not enabled on this server";
Note that if you're using localhost, a mail service is usually not installed by default and this will fail consistently. Either install one, or just use a live server environment on your host.
Change the last error message to:
//Disabled for now :)
//http_response_code(403);
echo "There was a problem with your submission: request was not POST.";
This should help you figure out your problem and get some better help from us. My guess is that 1) You do not have >= php 5.4 installed, and the mail() function is failing because you're on a local environment (your computer).
Good luck!
Max Carlquist
2,791 PointsHello all, I did forget to add the php. Here it is...
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "maximus@tenkaklet.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
Max Carlquist
2,791 Pointsand thank you Jonathan for pointing it out for me :D
Christopher Borchert
14,814 PointsCould you please check the php version on your server? The manual notes that you need php 5.4 or above to use the http_response_code() function. (http://php.net/manual/en/function.http-response-code.php)
A quick way to check your version of php, if you don't know where to check it, is make a new php file containing only the line: <?php phpinfo(); ?> That will echo out a page with a bunch of tables detailing your php install. Look for "PHP version" near the top of the page.
Hope this helps. If not, I'lllook a little closer at your code.
Max Carlquist
2,791 PointsHey again,
Thanks for the help here! I did look it up with my web hosting and they are in the middle of updating to php 5.4 :). So much thanks for the help!
//Max
Christopher Borchert
14,814 PointsGlad to be of assistance. I just ran into a similar error with Bluehost in the last week.
Max Carlquist
2,791 PointsAh that sucks. Hope it went well :).
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Max.
I'm afraid this is still a little beyond me at this stage of my PHP education, however if you post the code to the forum from your php file, I'm sure I and others could have a crack at it for you. :-)