电子邮件不会发送输入的信息,并在页面刷新时发送空白电子邮件

时间:2014-06-05 00:05:53

标签: php html email

我已经为我的联系表单完成了服务器端验证,当用户无法在必填字段中输入信息时,按下发送按钮并且没有发送电子邮件时会发生错误。如果所有相关信息都输入到字段中,我就无法收到要发送的电子邮件。表单经过验证并在同一页面上发送。我添加了正确的脚本来发送电子邮件,但在页面刷新时它会发送一封空白电子邮件并显示没有错误。

<?php
// define variables and set to empty values
  $firstnameErr = $secondnameErr = $emailaddressErr = $commentErr = $captchaErr = "";
  $firstname = $email = $secondname = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
 $firstnameErr = "First name is required";
} else {
 $firstname = test_input($_POST["firstname"]);
 // check if name only contains letters and whitespace
 if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
   $firstnameErr = "Invalid first name"; 
 }
}

 if (empty($_POST["secondname"])) {
 $secondnameErr = "Second name is required";
} else {
 $secondname = test_input($_POST["secondname"]);
 // check if e-mail address syntax is valid
 if (!preg_match("/^[a-zA-Z ]*$/",$secondname)) {
   $secondnameErr = "Invalid second name"; 
 }
}

if (empty($_POST["emailaddress"])) {
 $emailaddressErr = "Email address is required";
} else {
 $emailaddress = test_input($_POST["emailaddress"]);
 // check if e-mail address syntax is valid
 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$emailaddress)) {
   $emailaddressErr = "Invalid email format"; 
 }
}

if (empty($_POST["comment"])) {
 $commentErr = "Enter a message";
} else {
 $comment = test_input($_POST["comment"]);
 // check if name only contains letters and whitespace
 if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
   $commentErr = "Only letters and white space allowed"; 
 }
}

  if (empty($_POST["captcha"])) {
 $captchaErr = "Enter the answer to the sum";
} else {
 $captcha = test_input($_POST["captcha"]);
 // check if name only contains letters and whitespace
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<form name="Contact" form id="Contact" onsubmit=" return validate()" METHOD="POST"   action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">


<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name"    />
<span class="error"><?php echo $firstnameErr;?></span>    </div> 
<!--End input-->
</div> <!--End row--><br />
<div class="Row">
<div class="Lable">Second Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="secondname" class="detail" name="secondname" placeholder="Second Name"  />
<span class="error"><?php echo $secondnameErr;?></span>    </div> 
<!--End input-->
</div> <!--End row-->


<br />
<div class="Row">
<div class="Lable">Email Address:</div> <!--End of Lable-->
<div class="input">
<input type="email" id="emailaddress" class="detail" name="emailaddress" placeholder="Email Address"  />
<span class="error"><?php echo $emailaddressErr;?></span>     
</div> <!--End input-->
</div> <!--End row-->

    <br />
<div class="Row">
<div class="Lable">Your Message:</div> <!--End of Lable-->
<div class="input">
<textarea id="comment" name="comment" class="mess" placeholder="Your Message" minlength="10"  ></textarea>
<span class="error"><?php echo $commentErr;?></span>     
</div> <!--End input-->
</div> <!--End row--> 

<br />
<input id="number1" name="number1" readonly="readonly" class="Add" value="<?php echo rand(1,4) ?>" /> + 
<input id="number2" name="number2" readonly="readonly" class="Add" value="<?php echo rand(5,9) ?>" /> =
<input type="text" name="captcha" id="captcha" class="captcha" maxlength="2" />
<div class="Lable">Please give the correct answer to the sum</div>
<br />
<span class="captchaerror"><?php echo $captchaErr;?></span>     
<br />

<br />
<div class="submit">
<input type="submit" id="send" Name="send" value="Send"  />
</div><!--End of submit-->

<div class="Clear">
<input type="reset" id="clear" Name="Clear" value="Clear" />
</div>





</form>

<?php
if (!empty($_POST)) {

}
else {


$firstname = $_POST["firstname"];
$secondname = $_POST["secondname"];
$email = $_POST["emailaddress"];
$comments = $_POST["comment"];



 $message = "New Email for a customer" .



         "\r\nName of the contact" .
         "\r\n-". $firstname .
         "\r\nName of the contact" .
         "\r\n-". $secondname .
         "\r\nEmail address of the contact" . 
         "\r\n-".$email .
         "\r\nThe comment that the contact has made" . 
         "\r\n-".$comments .

 $headers =  "From: " . $email;


mail("kieran@localhost",$subject,$message,$headers);
$subjectReply = 'Thank you for your contact..';
$messageReply = 'You will soon receive an answer';
$headers = 'From: admin@shreddednutrition';

mail($email, $subjectReply, $messageReply, $headers);


}
?>

提前致谢

1 个答案:

答案 0 :(得分:0)

为什么:

if (!empty($_POST)) {

}
else {

所以基本上如果有一些$ _POST数据,代码什么也不做。

更改为:

if (empty($_POST)) {

}
else {