How make multiple checkbox items show in feedback form?


I have a feedback form which has several checkboxes. But I can’t make more than one of the checked boxes to actually show up in the mail.

Here’s how a snippet of the form looks (there are about 50 more boxes):

    <input type="checkbox" name="event" id="Bumperball" value="Bumperball" />
<input type="checkbox" name="event" id="Dancing" value="Dancing" />

The form is handled by a mailer:

$event        = $_POST["event"];
$date        = $_POST["date"];
$participants        = $_POST["participants"];
$name        = $_POST["name"];
$phone        = $_POST["phone"];
$company    = $_POST["company"];
$message    = $_POST["message"];
$from        = $_POST["from"];
$verif_box    = $_POST["verif_box"];

$msg_body =  "\n"
            ."Event........: " . $event . "\n"
            ."Date.............: " . $date . "\n"
            ."Participants...: " . $participants . "\n"
            ."Name.....: " . $name . "\n"
             ."Company..........: " . $company . "\n"
            ."Email............: " . $from . "\n"
            ."Phone...........: " . $phone . "\n"

…and it’s $event that I’d like to be able to accept multiple selected checkboxes.
(I hope I managed to explain… if not, please ask me to clarify…)


the way you define the checkbox names, they’re overwriting each other so you always end up with the last checked one.

use bracket/array notation for the names: name="event[]"

Can you explain further?
I tried that before, adding brackets in the form itself, but the mail did not include the selections, but just said “Array”.
So I suppose there’s something else I need to do, too…?

You could give them unique names:-

<input type="checkbox" name="bumperball" id="Bumperball" value="Bumperball" />
<input type="checkbox" name="dancing" id="Dancing" value="Dancing" />

Adding the brackets creates an array, to the data is in $events[0] and $events[1]

that’s what you get when you cast an array to a string. you need to convert the array of data properly into a string (e.g. via implode())

Thanks for reply.
Regretfully, I’m an absolute newbie to php and haven’t the slightest clue what you mean. :-S
Is there any chance that you can modify the code I posted above?

Thanks for reply.
Yes, I could give them unique names. But there are several dozens and I will then also need to add the same names in the mailer. This will guaranteed cause issues later on when there will be even more checkboxes, or different ones…
If you could be so kind and just show what you mean by editing the code I provided, that would be awesome…

If you create an array, you can use implode loke this:-

$events = implode(", ", $_POST['event']) ;

That will create a comma separated list from the array. You can change ", " to whatever you want to separate the list with.

1 Like

Thanks a million. Works like charm!

Just a tip for creating the form fields if there are a lot:-

    $fieldarr = array( // fill this with 50ish items
    foreach($fieldarr as $value) {
        echo '<p><label for="'.$value.'">'.$value.'</label><input type="checkbox" name="event[]" id="'.$value.'" value="'.$value.'" /></p>';

Good to know! Will absolutely try that one eventually.
Right now, I’m just happy that the form works… :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.