使用数组时,在电子邮件中显示来自$ _POST的变量

时间:2012-08-08 14:44:02

标签: php mailer

我正在尝试获取使用复选框进行多项选择的表单的结果。我在其他几个问题上讨论过使用数组,但是当我通过电子邮件发送变量结果时,它只给出了最后选择的结果。我确信我有错误的代码来显示变量。

代码视图:

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

4 个答案:

答案 0 :(得分:1)

我建议使用implode。为您节省了几行代码,并没有像foreach那样紧张:

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

答案 1 :(得分:0)

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

您实际上并未在此处为$options分配任何内容,只是将其打印出来。您将在电子邮件中显示最后一个选项,因为您在循环中设置的变量会保留其值。

尝试:

$myOptions = "";

if(!empty($_POST['options'])) {
    foreach($_POST['options'] as $options) {
        $myOptions .= $options; 
    }
}

然后:

Unit Options: $myOptions <br>

答案 2 :(得分:0)

尝试使用此代替代码。

$assembly = $_POST['assembly'];
$pump = $_POST['pump'];
$exchanger = $_POST['exchanger'];

$opt = "";
if(!empty($_POST['options'])) {
    foreach($_POST['options'] as $options) {
        $opt .= $options.",";
    }
    $opt = rtrim($opt, ","); //comma-separated checkbox values
}

$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: $opt <br>
Email: $email <br>
Name: $name <br>
Info: $info <br>

答案 3 :(得分:0)

你可以使用

$option=''; 

   if(!empty($_POST['options'])) {
            $option = implode(',', $_POST['options']);
    }

试试这个

$assembly = $_POST['assembly'];
$pump = $_POST['pump'];
$exchanger = $_POST['exchanger'];
$option = "";
if(!empty($_POST['options'])) {
    $option = implode(',', $_POST['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: $option <br>
Email: $email <br>
Name: $name <br>
Info: $info <br>