0

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

代码视图:

$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

4 回答 4

1

我建议使用implode. 为你节省了几行代码,而且不像foreach那么紧张:

$options = implode(", ", $_POST['options']);
Unit Options: $options <br>
于 2012-08-08T15:18:47.963 回答
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>
于 2012-08-08T14:50:16.203 回答
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>
于 2012-08-08T14:55:04.437 回答
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>
于 2012-08-08T14:47:59.650 回答