结合两个独立的阵列

时间:2013-01-12 23:47:59

标签: php arrays forms foreach

我的PHP邮件表单中有两个独立的数组,它们是不同的复选框: webdetails graphicdetails 。只会填写一个集合(另一个集合将被隐藏,除非选择了前一个单选按钮。)我想​​知道如何在我的PHP中正确引用它们。

这是我过去常常发布的内容,但不是两个:

foreach($_POST['webdetails'] as $value1) {
    $webdetails_msg .= "$value1\n";
}

如何在其中添加图形细节?复制代码段会给我 警告:为foreach()提供的参数无效

我对PHP的了解不多,所以我将不胜感激。

-

这是PHP我收到错误:

<?php
/* Set mail headers */
$myemail  = "my.email@gmail.com";
$subject    = "New Request";

$webdetails         = $_POST['webdetails'];
$graphicdetails     = $_POST['graphicdetails'];

foreach($_POST['webdetails'] as $value1) {
    $webdetails_msg .= "$value1\n";
}
foreach($_POST['graphicdetails'] as $value2) {
    $graphicdetails_msg .= "$value2\n";
}

/* Let's prepare the message for the e-mail */
$message = "
Details:
$webdetails_msg
$graphicdetails_msg
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

<?php exit(); } ?>

webdetails的HTML:

<input type="checkbox" name="webdetails[ ]" id="design" value="Design" />
<input type="checkbox" name="webdetails[ ]" id="development" value="Development" />
<input type="checkbox" name="webdetails[ ]" id="web-updates" value="Web Updates" />
<input type="checkbox" name="webdetails[ ]" id="forum-template" value="Forum Template (Full)" />
<input type="checkbox" name="webdetails[ ]" id="forum-graphics" value="Forum Graphics" />
<input type="checkbox" name="webdetails[ ]" id="web-other" value="Other" />

图形细节的HTML:

<input type="checkbox" name="graphicdetails[ ]" id="graphic-logo" value="Logo Design" />
<input type="checkbox" name="graphicdetails[ ]" id="graphic-social" value="Social Media Graphics" />
<input type="checkbox" name="graphicdetails[ ]" id="graphic-other" value="Other" />

1 个答案:

答案 0 :(得分:4)

它看起来很好所以我猜它是这样的:

如果未选中复选框,则不会发布复选框,您需要检查是否有

if (isset($_POST['graphicdetails'])){
    foreach($_POST['graphicdetails'] as $value1) {
        $webdetails_msg .= "$value1\n";
    }
}

但无论如何

if (isset($_POST['graphicdetails'])){
    $webdetails_msg .= implode("\n", $_POST['graphicdetails'])."\n";
} // you don't need the curly braces for 1 line but SO has a small code width

写得少