Php mail form multiple checkbox items

I’ve got a contact form that has multiple checkboxes. I’ve added [] to the name, but not sure what to do in the PHP. Without the [], I was only getting the first checkbox item to display in the email that was sent. After adding [], I get nada.

Here’s my code (I’ve truncated the HTML to only show the checkbox area):


<div class="form-check">
									<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox1" value="Newspaper/Magazine">
									<label for="checkbox1" class="form-check-label">Newspaper/Magazine</label>

								<div class="form-check">
									<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox1" value="Referral">
									<label for="checkbox2" class="form-check-label">Referral</label>
								<div class="form-check">
									<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox3" value="Builders Association">
									<label for="checkbox3" class="form-check-label">Builders Association</label>
								<div class="form-check"> 
									<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox4" value="Chamber of Commerce">
									<label for="checkbox4" class="form-check-label">Chamber of Commerce</label>


$name = $_POST['name'];
$email = $_POST['email'];
$findout = $_POST['findout'];
$method = $_POST['method'];
$message = $_POST['message'];
$subject = $_POST['subject'];
header('Content-Type: application/json');
if ($name === ''){
  print json_encode(array('message' => 'Name cannot be empty', 'code' => 0));
if ($email === ''){
  print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
} else {
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
  print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
if ($subject === ''){
  print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
if ($message === ''){
  print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));

$content="From: $name \nEmail: $email \nHow did you find out about us?: $findout \nPreferred way of contact: $method \nMessage: $message";
$recipient = "[email protected]";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Email successfully sent!', 'code' => 1));

Any help is appreciated.



Well, for multiple checkboxes with the same name and therefore, for options or something else.
You would give each of them a value and when you read the name you get the value.
Meaning, it’s an array…

if (isset($_POST['findout'])) {
    $all_findouts = $_POST['findout'];

And, $all_findouts is an array containing ALL of the checked ones. You would use a foreach or whatever
to use them at that point.

so I would put what you wrote at the top of the PHP and then replace $findout with $all_findout in the $content string?

Well, it depends on what you want to do with the data in the checkboxes. Since you show an email type of setup, you would need to display the values in a FOREACH as I mentioned… Like: (loosely…)
echo “You found us on:< br>”;
foreach($all_findouts as $one_findout) {
echo $one_findout . “< br>”;
Or something like that…

the end result is to have all content emailed in a contact form so you know who they are, how they found out about the company, what is a good way to connect and what the message is.

Well, in my humble opinion, you don’t care how they found you. But, some companies like to know this so they can target the advertising to the ones that are working.

Normally, you want them to pick one because only one really counts per person. So, I would use a radio button instead. This forces only one selection. And, makes the decoding of it simpler. No need to read a list of possibles, because only one will be checked. Make sense?

The content of the email should include who it is from, like a paragraph of why it is being sent to them. I see you started it with just a note saying “how did you find us”. They already know that since they checked off the box for it. I am assuming you are going to keep a counter for who selects what…

A few more comments on contact-us forms. If you leave it open as you have it on your server, hackers will bomb you with spam. You can add an extra input field with a value placed in it and they have to erase it before they can submit it. That way a robot spammer will not be able to send you spam. You just have to check like this: < input type=“text” name=“security” value=“Please erase this line! (keep out robots!)”>
then, in the PHP section, if (ISSET($_POST[“security”])) { echo an error because they did not erase; }

One further comment. When you get input from a posted form, you should filter it’s input. So, this:

$name = $_POST['name'];

Becomes like this:

$name = filter_input(INPUT_POST, 'name');

This keeps hackers from entering code into your input fields. You can Google this if you want more info.

Hope all this helps…

the site I am putting together is for someone else and they wish to know where their customers are coming from and where they aren’t, for marketing purposes. That’s why checkboxes and not radio buttons.

Good idea about security. I found the script and have been trying to adapt it.

Sponsor our Newsletter | Privacy Policy | Terms of Service