我尝试在网站上编写 php 表单,但收到错误 500。我无法弄清楚我做错了什么。你能看一下代码看看我做错了什么吗? PHP:
<?php
// define variables and set to empty values
$name = $email = $phone = $enquiry = "";
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" ) {
if ( empty( $_POST[ "name" ] ) ) {
$nameErr = "Name is required";
} else {
$name = test_input( $_POST[ "name" ] );
// check if name only contains letters and whitespace
if ( !preg_match( "/^[a-zA-Z-' ]*$/", $name ) ) {
$nameErr = "Only letters and white space allowed";
}
}
if ( empty( $_POST[ "email" ] ) ) {
$emailErr = "Email is required";
} else {
$email = test_input( $_POST[ "email" ] );
// check if e-mail address is well-formed
if ( !filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
$emailErr = "Invalid email format";
}
}
if ( empty( $_POST[ "phone" ] ) ) {
$comment = "";
} else {
$comment = test_input( $_POST[ "phone" ] );
}
if ( empty( $_POST[ "enquiry" ] ) ) {
$comment = "";
} else {
$comment = test_input( $_POST[ "enquiry" ] );
}
}
// Create the email and send the message
$destination = "[email protected]";
$subject = "Website Contact Form Enquiry: $name";
$body = "You have received a new message from your website contact form.\\n\\n"."Here are the details:\\n\\nName: $name\\n\\nEmail: $email\\n\\nPhone: $phone\\n\\nEnquiry:\\n$enquiry";
$header = "From: [email protected]\\n";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($destination, $subject, $message, implode("\r\n",
$headers));
// mail($to,$subject,$msg,$headers);
echo "Email successfully sent.";
?>
HTML 格式:
<form id="contact-form" method="post" action="/contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-10">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Name*" required="required" data-error="Your name is required." >
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Email*" required="required" data-error="Valid email is required." >
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-10">
<div class="form-group">
<input id="form_phone" type="text" name="phone" class="form-control" placeholder="Phone" >
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10">
<div class="form-group">
<textarea id="form_enquiry" name="enquiry" class="form-control" placeholder="Enquiry*" rows="6" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input class="btn btn-large btn-primary centre mt-10" type="submit" value="Submit" >
</div>
</div>
</div>
</form>
我已按照其他人的指示使表单正常工作,但所做的更改仍然会出现错误。 这是一个简单的形式,但我似乎对我做错了什么缺乏了解。 请帮助我。
如果您查看发送邮件的行,这是一个硬行结尾,将 $headers 推到新行上吗?这将调用 500 错误。
查看 /var/log/apache2/error.log(如果您使用的是 Debian)或 /var/log/httpd/error.log(如果使用的是 RHEL 或类似系统)。
您的代码存在许多问题,但首先关注快乐的道路,然后让事情正常运行。