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 = "[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

0

2 Answers 2

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
      }
  }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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>

3 Comments

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";
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']);
So now I have the following: <?php $name = $_POST['name']; $email = $_POST['email']; $findout = implode(", ",$_POST['findout']); $method = $_POST['method']; $message = $_POST['message']; $subject = $_POST['subject']; header('Content-Type: application/json'); and it still doesn't work. HTML checkboxes are like the following: <input type="checkbox" class="form-check-input" name="findout[]" id="checkbox1" value="Newspaper/Magazine"> <label for="checkbox1" class="form-check-label">Newspaper/Magazine</label>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.