PHP 联系表问题 - 400 错误,看不到错误
PHP Contact Form Issue - 400 error, can't see the fault
您好,我对此还比较陌生,我正在尝试使用我找到的模板创建一个带有联系表的网站。我已将其设置为 PHP 所能看到的最佳状态,但我不断收到 400 响应错误。我已经检查了模板代码并删除了明显的拼写错误,并尝试修复其他问题但无济于事。你能帮忙吗,因为我对 PHP 一点也不了解!
HTML形式:
<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>
和 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 = "Ienteredmyemailhere@myemail.com";
// 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.";
}
?>
谢谢。我想这可能相对容易,我只是看不出错误发生在哪里。
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;
}
在此代码中,您检查了 PHP 服务器未收到的 ($subject)。所以这个变量是空的,这个条件成立,你每次都会收到 400 条错误消息。
您可以将 $subject 更改为 $email(您已将其用作存储收到的电子邮件的变量)以更正您的程序。
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;
}
希望这能解决您的问题。
您好,我对此还比较陌生,我正在尝试使用我找到的模板创建一个带有联系表的网站。我已将其设置为 PHP 所能看到的最佳状态,但我不断收到 400 响应错误。我已经检查了模板代码并删除了明显的拼写错误,并尝试修复其他问题但无济于事。你能帮忙吗,因为我对 PHP 一点也不了解!
HTML形式:
<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>
和 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 = "Ienteredmyemailhere@myemail.com";
// 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.";
}
?>
谢谢。我想这可能相对容易,我只是看不出错误发生在哪里。
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;
}
在此代码中,您检查了 PHP 服务器未收到的 ($subject)。所以这个变量是空的,这个条件成立,你每次都会收到 400 条错误消息。 您可以将 $subject 更改为 $email(您已将其用作存储收到的电子邮件的变量)以更正您的程序。
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;
}
希望这能解决您的问题。