PHP: 变量双引号内的 if 或 foreach 语句
PHP: if or foreach statement inside a double quotes of a variable
我有一个表单可以将 table 发送到包含多个复选框数据的电子邮件。
...
<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 = "example@example.com";
$from = 'test@test.comu';
$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: <example@example.com>\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>";
}
?>
我在提交后在表单页面看到勾选的位置。但是在邮件 post 中,我看到 Styles 之后的空单元格,并且在该单元格之后没有看到任何单元格。所以我没有看到选中的位置和带有文本 "SOMETHING here" 等的单元格。哪里出错了?
您是在回显结果,而不是添加到邮件正文中。这应该足够了:
$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>";
改变这个...
echo "<span>". $stylish , ", ". "</span>";
到...
echo "<span>".$stylish.", "."</span>";
我有一个表单可以将 table 发送到包含多个复选框数据的电子邮件。
...
<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 = "example@example.com";
$from = 'test@test.comu';
$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: <example@example.com>\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>";
}
?>
我在提交后在表单页面看到勾选的位置。但是在邮件 post 中,我看到 Styles 之后的空单元格,并且在该单元格之后没有看到任何单元格。所以我没有看到选中的位置和带有文本 "SOMETHING here" 等的单元格。哪里出错了?
您是在回显结果,而不是添加到邮件正文中。这应该足够了:
$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>";
改变这个...
echo "<span>". $stylish , ", ". "</span>";
到...
echo "<span>".$stylish.", "."</span>";