Search code examples
phpformsvariablesforeachmessage

PHP: if or foreach statement inside a double quotes of a variable


I have a form that sends a table to email with a multiple checkbox data.

...
<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>                                                    

But I have a problem with a php-handler file, where I need to output a data from these checkboxes.

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

I see the checked positions on the form page after submitting. But in the mail post I see the empty cell after Styles and don't see any cells after that cell. So I don't see a checked positions and a cell with a text "SOMETHING here" and etc. Where is a mistake?


Solution

  • You're echoing the result instead of adding to the message body. This should suffice:

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