title: Posting multiple checkboxes using PHP and sending to an email
body: <p>I am fairly new with using PHP. I have a form that I am using to send a contact form form a webpage to an email address. </p>
  
  <p>I am having trouble passing multiple checkboxes styled with a comma. For example, if the user picks checkbox1 and checkbox4, the code will be styled: checkbox1, , , checkbox4. I don't want to display the commas, unless the user picks the checkboxes. Let me know if that makes sense or not. </p>
  
  <p>This is the HTML:</p>
  
  <pre><code>    &lt;code&gt;
  
  &lt;form id="contact_form" class="contact" method="post" action="email_process.php"&gt;
              &lt;input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required&gt;
              &lt;input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required&gt;&lt;br&gt;&lt;br&gt;
  
              &lt;input class="email text" name="email" type="email" placeholder="EMAIL:" required&gt;
              &lt;input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required&gt;&lt;br&gt;&lt;br&gt;
  
              &lt;label&gt;Would you like to be contacted by:&lt;/label&gt;
              &lt;input name="how" class="emailbtn radio" type="checkbox" value="Email"&gt;
              &lt;label&gt;EMAIL&lt;/label&gt;
              &lt;input name="how2" class="phonebtn radio" type="checkbox" value="Phone"&gt;
              &lt;label&gt;PHONE NUMBER&lt;/label&gt;&lt;br&gt;&lt;br&gt;
  
              &lt;div class="one"&gt;
              &lt;label class="margin"&gt;EVENT DATE:&lt;/label&gt;&lt;br&gt;
              &lt;input name="month" class="month small" type="number" placeholder="MM"&gt;
              &lt;input name="day" class="day small" type="number" placeholder="DD"&gt;
              &lt;input name="year" class="year small" type="number" placeholder="YYYY"&gt;
              &lt;/div&gt;
              &lt;div class="one"&gt;
              &lt;label class="margin"&gt;EVENT LOCATION:&lt;/label&gt;&lt;br&gt;
              &lt;input name="location" class="location text" type="text"&gt;
              &lt;/div&gt;
  
              &lt;label&gt;Services we may assist you with:&lt;/label&gt;&lt;br&gt;&lt;br&gt;
              &lt;div class="two"&gt;
              &lt;input name="services" class="chefbtn radio" type="checkbox" value="Personal Chef"&gt;
              &lt;label&gt;PERSONAL CHEF&lt;/label&gt;&lt;br&gt;
              &lt;input name="services2" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes"&gt;
              &lt;label&gt;PRIVATE COOKING CLASSES&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="two"&gt;
              &lt;input name="services3" class="chefbtn radio" type="checkbox" value="Event Catering"&gt;
              &lt;label&gt;EVENT CATERING&lt;/label&gt;&lt;br&gt;
              &lt;input name="services4" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting"&gt;
              &lt;label&gt;RESTERAUNT CONSULTING&lt;/label&gt;
              &lt;/div&gt;
  
              &lt;input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?"&gt;
              &lt;textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"&gt;&lt;/textarea&gt;
  
              &lt;input class="submit" type="image" src="../images/contact/s1_submit_btn.png"&gt;
          &lt;/form&gt;
      &lt;/code&gt;
  </code></pre>
  
  <p>This is my PHP:</p>
  
  <pre><code>    &lt;code&gt;
  
  &lt;?php
  
  //Prefedined Variables 
  $to = "Enter email here";
  
  // Email Subject
  $subject = "Contact from your website.";
  
  // This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
  if($_POST) {
  
  // Collect POST data from form
  $firstname = stripslashes($_POST['firstname']);
  $lastname = stripslashes($_POST['lastname']);
  $email = stripslashes($_POST['email']);
  $how = stripslashes($_POST['how']);
  $how2 = stripslashes($_POST['how2']);
  $phone = stripslashes($_POST['phone']);
  $month = stripslashes($_POST['month']);
  $day = stripslashes($_POST['day']);
  $year = stripslashes($_POST['year']);
  $location = stripslashes($_POST['location']);
  $services = stripslashes($_POST['services']);
  $services2 = stripslashes($_POST['services2']);
  $services3 = stripslashes($_POST['services3']);
  $services4 = stripslashes($_POST['services4']);
  $heard = stripslashes($_POST['heard']);
  $comments = stripslashes($_POST['comments']);
  
  if ( ! empty($how) &amp;&amp; ! empty($how2) ) { 
  $contact_type = $how.', '.$how2;
  } elseif (! empty($how)){
      $contact_type = $how;
  } elseif (! empty($how2)){
      $contact_type = $how2;
  }
  
  if ( ! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) { 
  $contact_type2 = $services. ', ' .$services2. ', ' .$services3. ', ' .$services4;
  }
  
  // Collecting all content in HTML Table
  $content='&lt;table width="100%"&gt;
  &lt;tr&gt;&lt;td  align "center"&gt;&lt;b&gt;Contact Details&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;First Name:&lt;/td&gt;&lt;td&gt; '.$firstname.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Last Name:&lt;/td&gt;&lt;td&gt; '.$lastname.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Email:&lt;/td&gt;&lt;td&gt; '.$email.' &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Phone:&lt;/td&gt; &lt;td&gt; '.$phone.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;How Should We Contact You?&lt;/td&gt; &lt;td&gt; '.$contact_type.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Event Date:&lt;/td&gt;&lt;td&gt; '.$month.' / '.$day.' / '.$year.' &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Location:&lt;/td&gt; &lt;td&gt; '.$location.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Which Services Are You Interested In?&lt;/td&gt; &lt;td&gt; '.$contact_type2.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;How Did You Hear About Us?&lt;/td&gt; &lt;td&gt; '.$heard.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Comments:&lt;/td&gt; &lt;td&gt; '.$comments.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Date:&lt;/td&gt; &lt;td&gt; '.date('d/m/Y').'&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt; ';
  
  
  // Define email variables
  $headers = "From:".$email."\r\n";
  $headers .= "Reply-to:".$email."\r\n";
  $headers .= 'Content-type: text/html; charset=UTF-8';
  
  if (! empty($how) || ! empty($how2)) {
  if (! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) {
  if( ! empty($firstname) &amp;&amp; ! empty($lastname) &amp;&amp; ! empty($email) &amp;&amp; ! empty($phone) &amp;&amp; ! empty($month) &amp;&amp; ! empty($day) &amp;&amp; ! empty($year) &amp;&amp; ! empty($location) &amp;&amp; ! empty($heard) &amp;&amp; ! empty($comments) ) {
  
  // Sending Email 
  if( mail($to, $subject, $content, $headers) ) {
  print "Thank you, I will get back to you shortly!&lt;br&gt;";
  return true;
  }
  else {
  print "An error occured and your message could not be sent.";
  return false;
  }
  }
  else {
  print "An error occured and your message could not be sent.";
  return false;
  }
  }
  }
  }
  
      &lt;/code&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After some research and the right guidance I came across an answer that helped me solve this. If anyone else is having the same problem feel free to refer to this. </p>
  
  <pre><code>    &lt;?php
  
  //Prefedined Variables 
  $to = "Enter email here";
  
  // Email Subject
  $subject = "Contact from your website.";
  
  // This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
  if($_POST) {
  
  // Collect POST data from form
  $firstname = stripslashes($_POST['firstname']);
  $lastname = stripslashes($_POST['lastname']);
  $email = stripslashes($_POST['email']);
  $how = 'None';
      if(isset($_POST['how']) &amp;&amp; is_array($_POST['how']) &amp;&amp; count($_POST['how']) &gt; 0){
      $selectedHow = implode(', ', $_POST['how']);
      }
  $phone = stripslashes($_POST['phone']);
  $month = stripslashes($_POST['month']);
  $day = stripslashes($_POST['day']);
  $year = stripslashes($_POST['year']);
  $location = stripslashes($_POST['location']);
  $services = 'None';
      if(isset($_POST['services']) &amp;&amp; is_array($_POST['services']) &amp;&amp; count($_POST['services']) &gt; 0){
      $selectedServices = implode(', ', $_POST['services']);
      }
  $heard = stripslashes($_POST['heard']);
  $comments = stripslashes($_POST['comments']);
  
  // Collecting all content in HTML Table
  $content='&lt;table width="100%"&gt;
  &lt;tr&gt;&lt;td  align "center"&gt;&lt;b&gt;Contact Details&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;First Name:&lt;/td&gt;&lt;td&gt; '.$firstname.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Last Name:&lt;/td&gt;&lt;td&gt; '.$lastname.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Email:&lt;/td&gt;&lt;td&gt; '.$email.' &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Phone:&lt;/td&gt; &lt;td&gt; '.$phone.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;How Should We Contact You?&lt;/td&gt; &lt;td&gt; '.$selectedHow.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Event Date:&lt;/td&gt;&lt;td&gt; '.$month.' / '.$day.' / '.$year.' &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Location:&lt;/td&gt; &lt;td&gt; '.$location.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Which Services Are You Interested In?&lt;/td&gt; &lt;td&gt; '.$selectedServices.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;How Did You Hear About Us?&lt;/td&gt; &lt;td&gt; '.$heard.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Comments:&lt;/td&gt; &lt;td&gt; '.$comments.'&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;Date:&lt;/td&gt; &lt;td&gt; '.date('d/m/Y').'&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt; ';
  
  
  // Define email variables
  $headers = "From:".$email."\r\n";
  $headers .= "Reply-to:".$email."\r\n";
  $headers .= 'Content-type: text/html; charset=UTF-8';
  
  if( ! empty($firstname) &amp;&amp; ! empty($lastname) &amp;&amp; ! empty($email) &amp;&amp; ! empty($phone)  &amp;&amp; ! empty($selectedHow) &amp;&amp; ! empty($month) &amp;&amp; ! empty($day) &amp;&amp; ! empty($year) &amp;&amp; ! empty($location) &amp;&amp; ! empty($selectedServices) &amp;&amp; ! empty($heard) &amp;&amp; ! empty($comments) ) {
  
  // Sending Email 
  if( mail($to, $subject, $content, $headers) ) {
  print "Thank you, I will get back to you shortly!&lt;br&gt;";
  return true;
  }
  else {
  print "Please go back and make sure that all fields have been filled out.";
  return false;
  }
  }
  else {
  print "An error occured and your message could not be sent.";
  return false;
  }
  }
  
  
  &lt;form id="contact_form" class="contact" method="post" action="email_process.php"&gt;
          &lt;input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required&gt;
          &lt;input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required&gt;&lt;br&gt;&lt;br&gt;
  
          &lt;input class="email text" name="email" type="email" placeholder="EMAIL:" required&gt;
          &lt;input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required&gt;&lt;br&gt;&lt;br&gt;
  
          &lt;label&gt;Would you like to be contacted by:&lt;/label&gt;
          &lt;input name="how[]" class="emailbtn radio" type="checkbox" value="Email"&gt;
          &lt;label&gt;EMAIL&lt;/label&gt;
          &lt;input name="how[]" class="phonebtn radio" type="checkbox" value="Phone"&gt;
          &lt;label&gt;PHONE NUMBER&lt;/label&gt;&lt;br&gt;&lt;br&gt;
  
          &lt;div class="one"&gt;
          &lt;label class="margin"&gt;EVENT DATE:&lt;/label&gt;&lt;br&gt;
          &lt;input name="month" class="month small" type="number" placeholder="MM"&gt;
          &lt;input name="day" class="day small" type="number" placeholder="DD"&gt;
          &lt;input name="year" class="year small" type="number" placeholder="YYYY"&gt;
          &lt;/div&gt;
          &lt;div class="one"&gt;
          &lt;label class="margin"&gt;EVENT LOCATION:&lt;/label&gt;&lt;br&gt;
          &lt;input name="location" class="location text" type="text"&gt;
          &lt;/div&gt;
  
          &lt;label&gt;Services we may assist you with:&lt;/label&gt;&lt;br&gt;&lt;br&gt;
          &lt;div class="two"&gt;
          &lt;input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef"&gt;
          &lt;label&gt;PERSONAL CHEF&lt;/label&gt;&lt;br&gt;
          &lt;input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes"&gt;
          &lt;label&gt;PRIVATE COOKING CLASSES&lt;/label&gt;
          &lt;/div&gt;
          &lt;div class="two"&gt;
          &lt;input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering"&gt;
          &lt;label&gt;EVENT CATERING&lt;/label&gt;&lt;br&gt;
          &lt;input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting"&gt;
          &lt;label&gt;RESTERAUNT CONSULTING&lt;/label&gt;
          &lt;/div&gt;
  
          &lt;input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?"&gt;
          &lt;textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"&gt;&lt;/textarea&gt;
  
          &lt;input class="submit" type="image" src="../images/contact/s1_submit_btn.png"&gt;
      &lt;/form&gt;
  </code></pre>