简体   繁体   中英

Combining Two Separate Arrays

I have two separate arrays in my PHP mail form that are different sets of checkboxes: webdetails and graphicdetails . Only one set will be filled out (the other will be hidden unless a previous radio button is selected.) I'm wondering how to reference them correctly in my PHP.

This is what I used to get one to post, but not two:

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

How do I add graphicdetails in there? Duplicating the snippet gives me Warning: Invalid argument supplied for foreach()

I don't know much more PHP than this, so an explanation would be appreciated.

--

Here's the PHP I get an error with:

<?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(); } ?>

HTML for webdetails:

<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 for graphicdetails:

<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" />

It looks fine so I am guessing it's this:

Checkboxes don't get posted if they are not checked, you need to check there are any

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

But in any case

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

is less to write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM