Hi!
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"
."MESSAGE.\n\n"
."Event........: " . $event . "\n"
."Date.............: " . $date . "\n"
."Participants...: " . $participants . "\n"
."Name.....: " . $name . "\n"
."Company..........: " . $company . "\n"
."Email............: " . $from . "\n"
."Phone...........: " . $phone . "\n"
."Message.....:\n\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…)
Cheers,
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[]"
Thanks!
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…?
SamA74
July 13, 2015, 2:10pm
4
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]
The_Doodle_Dandy:
I tried that before, adding brackets in the form itself, but the mail did not include the selections, but just said “Array”.
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…
SamA74
July 13, 2015, 3:13pm
8
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
Yay!!!
Thanks a million. Works like charm!
SamA74
July 13, 2015, 4:14pm
10
Just a tip for creating the form fields if there are a lot:-
<?php
$fieldarr = array( // fill this with 50ish items
'Bumperball',
'dancing',
'etc'
);
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…
system
Closed
October 13, 2015, 1:17am
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.