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):
html
<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>
<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>
<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>
<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>
</div>
PHP
<?php
$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));
exit();
}
if ($email === ''){
print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
exit();
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
exit();
}
}
if ($subject === ''){
print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
exit();
}
if ($message === ''){
print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
exit();
}
$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));
exit();
?>
Any help is appreciated.
Mahalo!
Chris