如何修复 php 联系表的未定义变量错误?

How do I fix this undefined variable error for a php contact form?

我每天大约在同一小时内收到空白电子邮件,并且在我的 php 邮件程序中收到未定义的变量错误,我需要做什么来修复它? 我的页面中有 2 种不同的形式。

我尝试将其添加到我的 php: if (isset($_POST)){},添加后我仍然得到空白,但现在的错误是针对不同的行。现在我的错误是 $email_subject $email_body 行 35,36.

这是错误日志:

[2019 年 9 月 19 日 19:26:50 UTC] PHP 注意:未定义变量:第 35 行 /home/user/public_html/mail/mailer.php 中的名称 [2019 年 9 月 19 日 19:26:50 UTC] PHP 注意:未定义变量:第 36 行 /home/user/public_html/mail/mailer.php 中的名称

我的PHP:

if (isset($_POST['name'])) {
    $name = $_POST['name'];
}

if (isset($_POST['fname'])) {
    $fname = $_POST['fname'];
}

if (isset($_POST['lname'])) {
    $lname = $_POST['lname'];
}

if (isset($_POST['email'])) {
    $email = $_POST['email'];
}

if (isset($_POST['tel'])) {
    $tel = $_POST['tel'];
}

if (isset($_POST['address'])) {
    $address = $_POST['address'];
}

if (isset($_POST['project'])) {
    $project = $_POST['project'];
}

if (isset($_POST['message'])) {
    $message = $_POST['message'];
}

    $to = '';
    $email_subject = "$fname $lname $name Contact You from :";
    $email_body = "You have received a new message from your website contact form.<br/>"."Here are the details:<br/><br/> Name: $fname $lname $name<br/> Email: $email<br/> Phone: $tel<br/> Address: $address<br/> Project: $project<br/> Message:\n$message";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: ' . "\r\n";
    $headers .= 'Reply-To: ' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $name = $fname = $lname = $email = $tel = $address = $project = $message = '';

    if(mail($to,$email_subject,$email_body,$headers)) {
            echo "OK";
        }

在没有看到错误消息的情况下,我猜想并非所有字段都已填写,因此您需要在检查它们是否已设置之前将这些变量设置为默认值:

$name = $fname = $lname = $email = $tel = $address = $project = $message = '';