Hi I am still relatively new to this, and am trying to create a site with a contact form from a template I found. I have set it up as best as I can see with the PHP, but I keep getting a 400 response error. I have gone through the template code and removed the obvious spelling mistakes, and tried to fix other things but to no avail. Can you help as I don't really know much about PHP at all!
HTML form:
<form id="contact-form" action="assets/contact.php" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-input mt-25">
<label>Name</label>
<div class="input-items default">
<input name="name" id="name" type="text" placeholder="Name">
<i class="lni lni-user"></i>
</div>
</div> <!-- form input -->
</div>
<div class="col-md-6">
<div class="form-input mt-25">
<label>Email</label>
<div class="input-items default">
<input type="email" id="email" name="email" placeholder="Email">
<i class="lni lni-envelope"></i>
</div>
</div> <!-- form input -->
</div>
<div class="col-md-12">
<div class="form-input mt-25">
<label>Message</label>
<div class="input-items default">
<textarea name="message" id="message" placeholder="Message"></textarea>
<i class="lni lni-pencil-alt"></i>
</div>
</div> <!-- form input -->
</div>
<p class="form-message"></p>
<div class="col-md-12">
<div class="form-input light-rounded-buttons mt-30">
<button class="main-btn light-rounded-two" type="submit" name="submit">Send Message</button>
</div> <!-- form input -->
</div>
</div> <!-- row -->
</form>
And the PHP (contact.php)
<?php
// 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($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "[email protected]";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "First 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.";
}
?>
Thanks. I assume it is probably relatively easy, I just can't see where the error is happening.
if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
Here in this code, you have checked for ($subject) which is not received by the PHP server. So this variable is empty and this condition comes to be true and you get 400 error message every time. You can change $subject to $email (which you have used as a variable to store the received email) to correct your program.
if ( empty($name) OR empty($email) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}
Hope this will solve your problem.