PHP联系表格空白数据&未知的发件人

时间:2015-08-01 13:48:24

标签: php html forms

您好我已在我的网站上发了一个联系表格,但即使用户没有输入任何详细信息并按下提交按钮,它也会向我发送一封空白电子邮件,其中没有任何内容来自未知发件人。

任何人都知道为什么会这样吗?我已经添加了表单验证,因此它不应该发送任何内容。

HTML代码:

<form action="mail.php" method="POST">
<font color="red">*</font> Name <input type="text" name="name" required>
<font color="red">*</font> Phone <input type="text" name="phone" required>
<font color="red">*</font> Email <input type="text" name="email" required>
<font color="red">*</font> Message <input type="text" name="message" placeholder="I am looking for..." required><br />
<input type="image" src="images/Landing_Pages/submit.png" border="0" alt="Submit" />
</form>

PHP代码:

<?php $name = $_POST['name'];
    $phone = $_POST['phone'];
    $preferred = $_POST['preferred'];
    $email = $_POST['email'];
    $formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
    $recipient = "bwebb@webbmaster.com.au";
    $subject = "New Request Southbank";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    ?>

Image

2 个答案:

答案 0 :(得分:4)

您必须检查帖子数据是否为空

 if (!empty($name) && !empty($phone) && !empty($preferred) && !empty($email)) {
      mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
 }

修改

<?php
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $preferred = $_POST['preferred'];
    $email = $_POST['email'];
    $formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
    $recipient = "bwebb@webbmaster.com.au";
    $subject = "New Request Southbank";
    $mailheader = "From: $email \r\n";
    if (!empty($name) && !empty($phone) && !empty($message) && !empty($preferred) && !empty($email)) {
        mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    }
?>

答案 1 :(得分:1)

做类似的事情:

$errorflag = 0 ;
if(empty($name)){$errorflag = 1 ;$error = "Input your Name plz!"; }; 
if(empty($phone)){$errorflag = 1 ;$error = "Input phone"; }
...
if(!$errorflag) {
    @mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}

检查您需要的每个输入,然后发送