Search code examples
phpmailer

Display variable from $_POST in email when using an array


I am trying to get the results of a form that uses a check box for multiple selections. I used an array as discussed on several other questions, but when i send the variables results through email it only gives me the last result selected. I am sure I have the wrong code for displaying the variable.

Code View:

$assembly = $_POST['assembly'];
$pump = $_POST['pump'];
$exchanger = $_POST['exchanger'];
**if(!empty($_POST['options'])) {
foreach($_POST['options'] as $options) {
        echo $options; 
}
}**

$name = $_POST['name'];
$email = $_POST['email'];
$info = $_POST['info'];

$body = <<<EOD
<br><hr><br>
Assembly: $assembly <br>
Pump: $pump <br>
Heat Exchanger: $exchanger <br>
**Unit Options: $options <br>**
Email: $email <br>
Name: $name <br>
Info: $info <br>

Solution

  • I would suggest the use of implode. Saves you a few line of code, and is not as stressful as the foreach:

    $options = implode(", ", $_POST['options']);
    Unit Options: $options <br>