我有一个表单,可以将包含多个复选框数据的表格发送到电子邮件。
...
<ul>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Английская классика" />Английская классика</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Итальянская классика" />Итальянская классика</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Кантри" />Кантри</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Прованс" />Прованс</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Деревенская кухня" />Деревенская кухня</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Современная классика" />Современная классика</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Скандинавский минимализм" />Скандинавский минимализм</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Эко-кухня" />Эко-кухня</li>
<li><input id="const-style" class="checkbox-form" type="checkbox" name="style[]" value="Старинный Рустикаль" />Старинный Рустикаль</li>
</ul>
但是我的 php-handler 文件有问题,我需要从这些复选框输出数据。
if(!empty($_POST['name'] ))
{
$to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dcc1d8d4c9d5dcf9dcc1d8d4c9d5dc97dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>";
$from = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="364253454276425345421855595b43" rel="noreferrer noopener nofollow">[email protected]</a>';
$subject = "Form-test";
$kit_styles = $_POST['style'];
$message = "
<html>
<head>
<title></title>
</head>
<body>
<table border='1' width='300px;'>
...
<tr>
<td>Styles</td>
<td> "?><?php foreach ($kit_styles as $stylish) {echo "<span>". $stylish , ", ". "</span>";}" </td>
</tr>
<tr><td> SOMETHING here </td><td>and here</td></tr>
...
</table>
</body>
</html>";
$content = "text/plain";
$headers = "Content-type: text/html; charset=UTF-8 \r\n";
$headers .= "From: <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91f4e9f0fce1fdf4d1f4e9f0fce1fdf4bff2fefc" rel="noreferrer noopener nofollow">[email protected]</a>>\r\n";
$result = mail($to, $subject, $message, $headers);
if ($result){
echo "<div id='constructResult' class='form-text success inline'>Sent!</div>";
}
else{
echo "<div id='constructResult' class='form-text failed inline'>Didn't send. Try again</div>";
}
}
else {
echo "<div id='constructResult' class='form-text failed inline'>A fields with <span style='color:red;'>*</span> are empty.</div>";
}
?>
提交后,我在表单页面上看到了选中的职位。但在邮件中,我在样式后面看到了空单元格,并且在该单元格后面没有看到任何单元格。所以我没有看到选中的位置和带有文本“这里有东西”等的单元格。哪里有错误?
最佳答案
您正在回显结果,而不是添加到消息正文。这应该足够了:
$message = "
<html>
<head>
<title></title>
</head>
<body>
<table border='1' width='300px;'>
...
<tr>
<td>Styles</td>
<td> ";
foreach ($kit_styles as $stylish) {
$message .= "<span>$stylish, </span>";
}
$message .= " </td>
</tr>
<tr><td> SOMETHING here </td><td>and here</td></tr>
...
</table>
</body>
</html>";
关于PHP:变量的双引号内的 if 或 foreach 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843695/