title: php email form. Only want to post filled fields and not post empty ones
body: <p>I have a contact form that looks like <a href="http://carine.zenwebcreative.com.au/contact.html" rel="nofollow noreferrer">this</a></p>
  
  <p>Different form fields will appear based on what the user picks on the <strong>"I would like to"</strong> select box.</p>
  
  <p>I want to implement a <a href="https://stackoverflow.com/questions/18987622/send-only-field-boxes-filled-if-empty-then-do-not-send-in-email-php-form-to-em">solution</a> that I found on this site. Basically the php only posts fields that are filled out and does not post fields that are empty. It utilses the following code to achieve it</p>
  
  <pre><code>foreach ($_POST as $Field=&gt;$Value) { 
  if($Value != ''){
  $body .= "$Field: $Value\n";
  }
  }
  </code></pre>
  
  <p>I am not fluent in PHP and wasn't able to apply the solution proposed. Would appreciate if someone could help implement the solution into my code to make it post non empty form elements which includes text fields, select boxes and radio groups.</p>
  
  <p>The code for my PHP form processor looks like the following</p>
  
  <pre><code>&lt;?php
  
  $name = $_POST['name'];  
  $email_address = $_POST['email'];  
  $phone = $_POST['phone'];  
  $message = $_POST['message'];  
  $p_prefered_day = $_POST['p_prefered_day'];  (radio button)
  $session_time = $_POST['session_time'];  (select box)
  $p_message = $_POST['p_message'];  
  $treatment = $_POST['treatment'];  (select box)
  $therapist = $_POST['therapist'];  (select box)
  $b_pref_day = $_POST['b_pref_day'];  (radio button)
  $b_pref_time = $_POST['b_pref_time'];  (radio button)
  $b_message = $_POST['b_message'];  
  
  $to = 'info@zenwebcreative.com.au'; 
  $email_subject = "Website Contact Form:  $name";
  $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nCompany: $company\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: noreply@yourdomain.com\n";
  $headers .= "Reply-To: $email_address"; 
  
  mail($to,$email_subject,$email_body,$headers);
  return true;            
  ?&gt;
  </code></pre>
  
  <p>The HTML code of my form looks like the following</p>
  
  <pre><code>  &lt;form name="sentMessage" id="contactForm" novalidate&gt;                
      &lt;div class="form-group"&gt;
        &lt;label&gt;Full Name *&lt;/label&gt;
        &lt;input type="text" class="form-control" id="name" required&gt;
      &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
      &lt;/div&gt;&lt;!--  end form-group --&gt;
  
      &lt;div class="form-group"&gt;
        &lt;label&gt;Email Address*&lt;/label&gt;
        &lt;input type="email" class="form-control" id="email" required&gt;
      &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
      &lt;/div&gt;&lt;!--  end form-group --&gt;
  
      &lt;div class="form-group"&gt;
        &lt;label&gt;Contact Number&lt;/label&gt;
        &lt;input type="text" class="form-control" id="phone"&gt;
      &lt;/div&gt; &lt;!-- end form group --&gt;
  
      &lt;div class="form-group"&gt;
      &lt;label&gt;I would like to *&lt;/label&gt;
        &lt;select id="select_question" class="form-control"&gt;            
          &lt;option selected="selected"&gt;Please select&lt;/option&gt;
          &lt;option value="question"&gt;Ask a question&lt;/option&gt;
          &lt;option value="pilates"&gt;Attend a pilates class&lt;/option&gt;
          &lt;option value="booking"&gt;Schedule a booking&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
      &lt;div class="form-group box question"&gt;
      &lt;label&gt;Message&lt;/label&gt;
        &lt;div class="input-group"&gt;                  
          &lt;textarea id="message" class="form-control" rows="7" required&gt;&lt;/textarea&gt;
          &lt;span class="input-group-addon"&gt;&lt;span class="glyphicon glyphicon-asterisk"&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;/div&gt;&lt;!--  end input group --&gt;
      &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
      &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
      &lt;div class="box pilates"&gt;
  
        &lt;div class="form-group"&gt;
          &lt;label&gt;Prefered day of the week *&lt;/label&gt;&lt;br&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="p_prefered_day" id="Tue" value="Tuesday"&gt;Tuesday
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="p_prefered_day" id="Thu" value="Thursday"&gt;Thursday
          &lt;/label&gt;
        &lt;/div&gt; &lt;!-- end form group --&gt;
  
        &lt;div class="form-group"&gt;
        &lt;label&gt;Prefered Session you would like to attend? *&lt;/label&gt;
          &lt;select class="form-control" id="session_time"&gt;
            &lt;option value="4:30 pm"&gt;4:30 pm&lt;/option&gt;
            &lt;option value="5:30 pm"&gt;5:30 pm&lt;/option&gt;
            &lt;option value="6:30 pm"&gt;6:30 pm&lt;/option&gt;
          &lt;/select&gt;
        &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
        &lt;div class="form-group"&gt;
        &lt;label&gt;Additional Comments&lt;/label&gt;
          &lt;div class="input-group"&gt;                  
            &lt;textarea id="p_message" class="form-control" rows="7" required&gt;&lt;/textarea&gt;
            &lt;span class="input-group-addon"&gt;&lt;span class="glyphicon glyphicon-asterisk"&gt;&lt;/span&gt;&lt;/span&gt;
          &lt;/div&gt;&lt;!--  end input group --&gt;
        &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
        &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
      &lt;/div&gt;&lt;!--  end box pilates --&gt;
  
      &lt;div class="box booking"&gt;
  
        &lt;div class="form-group"&gt;
        &lt;label&gt;What Service Are You Interested In? *&lt;/label&gt;
          &lt;select class="form-control" id="services"&gt;
            &lt;option value="Manual Therapy"&gt;Manual Therapy&lt;/option&gt;
            &lt;option value="Sports Physiotherapy"&gt;Sports Physiotherapy&lt;/option&gt;
            &lt;option value="Dry Needling"&gt;Dry Needling&lt;/option&gt;
            &lt;option value="Post and Pre operative care"&gt;Post and Pre operative care&lt;/option&gt;
            &lt;option value="Real Time Ultrasound Imaging"&gt;Real Time Ultrasound Imaging&lt;/option&gt;
            &lt;option value="Balance and Vestibular treatment"&gt;Balance and Vestibular treatment&lt;/option&gt;
            &lt;option value="Sports Specific Rehabilitation"&gt;Sports Specific Rehabilitation&lt;/option&gt;
            &lt;option value="Motor Vehicle Accident"&gt;Motor Vehicle Accident&lt;/option&gt;
            &lt;option value="Workers Compensation"&gt;Workers Compensation&lt;/option&gt;            
          &lt;/select&gt;
        &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
        &lt;div class="form-group"&gt;
        &lt;label&gt;Which therapist would you like to see*&lt;/label&gt;
          &lt;select class="form-control" id="therapist"&gt;
            &lt;option selected="selected"&gt;No Preference&lt;/option&gt;
            &lt;option value="Kristen"&gt;Kristen&lt;/option&gt;
            &lt;option value="Tara"&gt;Tara&lt;/option&gt;        
          &lt;/select&gt;
        &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
        &lt;div class="form-group"&gt;
          &lt;label&gt;Prefered day of the week *&lt;/label&gt;&lt;br&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_day" id="b_Mon" value="Mon-"&gt;Mon
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_day" id="b_Tue" value="Tue"&gt;Tue
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_day" id="b_Wed" value="Wed"&gt;Wed
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_day" id="b_Thu" value="Thu"&gt;Thu
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_day" id="b_Fri" value="Fri"&gt;Fri
          &lt;/label&gt;
        &lt;/div&gt; &lt;!-- end form group --&gt; 
  
        &lt;div class="form-group"&gt;
          &lt;label&gt;Prefered time of the day&lt;/label&gt;&lt;br&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_time" id="morning" value="Morning"&gt;Morning
          &lt;/label&gt;
          &lt;label class="radio-inline"&gt;
            &lt;input type="radio" name="b_pref_time" id="afternoon" value="Afternoon"&gt;Afternoon
          &lt;/label&gt;
        &lt;/div&gt; &lt;!-- end form group --&gt; 
  
        &lt;div class="form-group"&gt;
        &lt;label&gt;Addtional Comments&lt;/label&gt;
          &lt;div class="input-group"&gt;                  
            &lt;textarea id="b_message" class="form-control" rows="7" required&gt;&lt;/textarea&gt;
            &lt;span class="input-group-addon"&gt;&lt;span class="glyphicon glyphicon-asterisk"&gt;&lt;/span&gt;&lt;/span&gt;
          &lt;/div&gt;&lt;!--  end input group --&gt;
        &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
        &lt;/div&gt;&lt;!--  end form-group --&gt; 
  
     &lt;/div&gt; &lt;!-- end box booking --&gt;
  
    &lt;div class="clearfix"&gt;&lt;/div&gt;
  
      &lt;div id="success"&gt;&lt;/div&gt;
      &lt;button id="send_btn" type="submit" class="btn cpcbutton"&gt;Send Message&lt;/button&gt;
      &lt;p class="help-block"&gt; &lt;span class="glyphicon glyphicon-asterisk green"&gt;&lt;/span&gt; Required Fields &lt;/p&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>