0

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.

(NOTE: I've tried using similar questions in StackOverflow, but they didn't work for my dilemma.)

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="checkbox5" value="Advertisement">
  <label for="findout[]" class="form-check-label">Advertisement</label>
</div>

<div class="form-check">
  <input type="checkbox" class="form-check-input" name="findout[]" id="checkbox6" value="Yard Sign">
  <label for="findout[]" class="form-check-label">Yard Sign</label>
</div>

<div class="form-check">
  <input type="checkbox" class="form-check-input" name="findout[]" id="checkbox7" value="Search Engine">
  <label for="findout[]" class="form-check-label">Search Engine</label>
</div>
<?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 = "emailaddress@domain.com";
    $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

Chris J. Popp
  • 25
  • 2
  • 7

2 Answers2

1

So people can select multiple check boxes and you want to read them correct? You can use the code below

<?php
  if(isset($_POST['findout'])){ // Check if findout had been posted
      foreach($_POST['findout'] as $checked){ // loop trough selected checkboxes
          echo $checked."</br>"; // Display selected checkboxes
      }
  }
?>
Crezzur
  • 1,303
  • 2
  • 18
  • 37
  • Tried your code (placed it at the start of my php) and the checkbox items ($findout) did not display in the email that was sent. Placed $findout in $content and did not work. Changed $findout to $checked and got the same result of no checkbox content displayed in the email. – Chris J. Popp Aug 19 '20 at 21:08
1

Adding the brackets to the field's name makes it an array, so you should access it like an array

$findout = implode(", ",$_POST['findout']);
echo $findout; // should output: Advertisement, Yard Sign, or whatever they select

Also your label's for attribute should point to your field's id, not the field's name

<label for="checkbox7" class="form-check-label">Search Engine</label>
Zurupupz
  • 335
  • 2
  • 12
  • Tried this, but the items checked still did not display in the email. In the PHP, I have for the email content: $content="From: $name \nEmail: $email \nHow did you find out about us?: $findout \nPreferred way of contact: $method \nMessage: $message"; – Chris J. Popp Aug 19 '20 at 21:20
  • If you put right at the top of the php it will get overwrited by `$findout = $_POST['findout'];` so replace that line with `$findout = implode(", ",$_POST['findout']);` – Zurupupz Aug 19 '20 at 21:49
  • So now I have the following: – Chris J. Popp Aug 20 '20 at 21:00