首页 > 解决方案 > 使 phpmailer 不使用 if 语句回显空输入

问题描述

我有一个带有输入的表单,如果客户不知道某些内容,他将字段留空并提交表单,然后发送一封电子邮件,他们可以根据表单获得相当长的时间。所以 IM 试图做到这一点,所以当输入为空时,它不会在 phpmailer 中回显它。但是,我不确定该怎么做,所以这就是我想出的。我究竟做错了什么?

 // defines how message looks in email
$mail->Body="
<html>
<head>
</head>
<body>
<center>

<span style='color:red;'>This customer needs a repair quote. Lets not keep him waiting!</span>
<div style='width:750px;text-align:center;'>


<div style='float:;'>
<span style='font-size:px;'><b>Personal Info</b><br></span>
<br>
<span style='font-size:px;'>---------------<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Full Name: </span>$name<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Phone Number:</span> $phone<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Email:</span> $email<br></span>
</div>
<br>
<div style='float:;'>
<span style='font-size:px;'><b>Vehicle Info</b><br></span>
<br>
<span style='font-size:px;'>---------------<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vehicle make:</span> $make<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vehicle Model:</span> $model<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Year:</span> $year<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vin:</span> $vin<br></span>






if (empty($_POST[$message])) {
// Evaluates post if empty moves on. If somone puts text in in echos it out
// do nothing or move to next line of code
echo '';
} else {
    echo '<br><span style='font-size:px;color:red;'>message:</span>$message<br></span>';
   




</div>
</div>
</center>
</body>
</html> 


";

标签: phpphpmailer

解决方案


尝试这个:

<?php 
$tbody = "
<html>
<head>
</head>
<body>
<center>

<span style='color:red;'>This customer needs a repair quote. Lets not keep him waiting!</span>
<div style='width:750px;text-align:center;'>


<div style='float:;'>
<span style='font-size:px;'><b>Personal Info</b><br></span>
<br>
<span style='font-size:px;'>---------------<br></span>
<br>";
if (!empty($_POST["name"])) {
$tbody .= "
<span style='font-size:px;'><span style='font-size:px;color:red;'>Full Name: </span>$name<br></span>
<br>
";
}

if (!empty($_POST["phone"])) {
$tbody .= "
<span style='font-size:px;'><span style='font-size:px;color:red;'>Phone Number:</span> $phone<br></span>
<br>";
}


if (!empty($_POST["email"])) {
$tbody .=  "
<span style='font-size:px;'><span style='font-size:px;color:red;'>Email:</span> $email<br></span>
";
}

$tbody .= "</div>
<br>
<div style='float:;'>
<span style='font-size:px;'><b>Vehicle Info</b><br></span>
<br>
<span style='font-size:px;'>---------------<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vehicle make:</span> $make<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vehicle Model:</span> $model<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Year:</span> $year<br></span>
<br>
<span style='font-size:px;'><span style='font-size:px;color:red;'>Vin:</span> $vin<br></span>

</div>
</div>
</center>
</body>
</html> 


";
$mail->Body = $tbody;
?>
  • 我只对三个参数添加了检查,但您可以随心所欲地进行检查。
  • .=标志手段add the next string to the previous string

推荐阅读