【问题标题】:PHP: if or foreach statement inside a double quotes of a variablePHP:变量的双引号内的if或foreach语句
【发布时间】:2016-11-28 11:55:01
【问题描述】:

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

...
<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 之后的空单元格,并且在该单元格之后看不到任何单元格。所以我没有看到选中的位置和带有“这里有东西”等文本的单元格。哪里有错误?

【问题讨论】:

    标签: php forms variables foreach message


    【解决方案1】:

    您是在回显结果,而不是添加到消息正文中。这应该足够了:

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

    【讨论】:

    • 谢谢!你的方法对我有用。我认为这是回声的问题,但我不知道如何避免这种情况。
    【解决方案2】:

    改变这个...

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

    到...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多