2016-11-28 35 views
2

我有一個表格將表格發送到帶有多個複選框數據的電子郵件。PHP:在變量的雙引號內的if或foreach語句

... 
<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處理程序文件的問題,我需要從這些複選框輸出數據。

if(!empty($_POST['name'])) 
{ 
$to = "[email protected]"; 
$from = '[email protected]'; 
$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: <[email protected]mple.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之後的空單元格,並且在該單元格之後沒有看到任何單元格。所以我沒有看到一個檢查過的位置和一個帶有文字「SOMETHING here」等的單元格。哪裏出錯?

回答

2

您迴應的結果,而不是添加到郵件正文。這應該足夠了:

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

謝謝!你的方式爲我工作。我認爲這是一個回聲問題,但我不知道如何避免這種情況。 – Tesla

1

改變了...

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

到...

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