出于某种原因,我的联系表单的后端代码中一直出现错误
这是我的联系表格
<div id = "form">
<form action ="contact2.php" method="post">
Hi Rebekah My Name Is
<br>
<input type="text" name="name">
<br>
My Email Is
<br>
<input type="text" name="email">
<br>
My Message Is
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>
<br>
<input type="submit" value="Submit">
<?php
include "contact2.php";
echo $result;
?>
</div>
这是我的后端PHP代码
<?php
$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'rebekahshaw92@yahoo.co.uk';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
// Check if name has been entered
if (!$_POST['name']) {
$field_name = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$field_message = 'Please enter your message';
}
// If there are no errors, send the email
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
?>
当我运行代码时,据说我的php代码在第43行是一个错误,这是最后一行,但我似乎无法看到问题是什么,因为这只是失败的标记PHP代码,当我拿出关闭php标签时,我仍然得到相同的消息。
以下是显示的消息。
解析错误:语法错误,第43行D:\ contact2.php中意外的文件结尾
出于安全原因,我已经从上面找到了文件的位置。
答案 0 :(得分:1)
您的if (!$field_name && !$email && !$field_message) {
未关闭。
答案 1 :(得分:1)
您缺少if
声明的结束标记。
在最后一行的结束标记}
之前添加?>
。
答案 2 :(得分:1)
您上次的IF声明缺少}
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} // <-----