I created a contact form based on this tutorial: http://blog.teamtreehouse.com/create-ajax-contact-form
I am using PHP Version 5.3.10-1ubuntu3.4 on my server and am having problems with http_response_code(); , which is used in the example tutorial in the link above. I read http_response_code(); only works with PHP 5.4. So instead, I returned to using header(); .
My form works very well for me, and it shows a success message when I submit, and not errors when I used http_response_code(); but my PHP is not so good and I want to know that what I did is acceptable or if I have to do it differently? If so, correct my code.
Here is the contents of my mailer.php file, where you can see that I commented on http_response_code(); and use header(); .
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); $phone = trim($_POST["phone"]); $company = trim($_POST["company"]); $minbudget = trim($_POST["minbudget"]); $maxbudget = trim($_POST["maxbudget"]); $message = trim($_POST["message"]); $deadline = trim($_POST["deadline"]); $referred = trim($_POST["referred"]); // Check that data was sent to the mailer. if ( empty($name) OR empty($phone) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { // Set a 400 (bad request) response code and exit. //http_response_code(400); header("HTTP/1.1 400 Bad Request"); echo "Error (400). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; exit; } // Set the recipient email address. // FIXME: Update this to your desired email address. $recipient = " myemail@domain.com "; // Set the email subject. $subject = "Website enquiry from $name"; // Build the email content. $email_content = "Name: $name\n"; $email_content .= "Email: $email\n\n"; $email_content .= "Phone: $phone\n"; $email_content .= "Company: $company\n\n"; $email_content .= "Budget: $minbudget $maxbudget\n"; $email_content .= "Deadline: $deadline\n"; //$email_content .= "Max Budget: $maxbudget\n"; $email_content .= "\n$message\n\n"; $email_content .= "Referred: $referred\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); header("HTTP/1.1 200 OK"); echo "Thank You! I'll be in touch soon."; } else { // Set a 500 (internal server error) response code. //http_response_code(500); header("HTTP/1.0 500 Internal Server Error"); echo "Error (500). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; } } else { // Not a POST request, set a 403 (forbidden) response code. //http_response_code(403); header("HTTP/1.1 403 Forbidden"); echo "Error (403). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; }
php
Glen
source share