PHP:if或foreach语句在变量的双引号内

时间:2016-11-28 11:55:02

标签: php forms variables foreach message

我有一个表单,可以使用多个复选框数据将表格发送到电子邮件。

...
<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>";
}
?>

我在提交后在表单页面上看到了已检查的位置。但是在邮件帖子中,我看到了Styles之后的空单元格,并且在该单元格之后看不到任何单元格。因此,我没有看到一个已检查的位置和一个带有文本的单元格,并且在这里有一些东西&#34;等等。哪里出错?

2 个答案:

答案 0 :(得分:3)

您回复结果而不是添加到邮件正文。这应该足够了:

$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>"; 

答案 1 :(得分:1)

改变这个......

echo "<span>". $stylish , ", ". "</span>";

为...

 echo "<span>".$stylish.", "."</span>";