title: PHPMailer Form Sending with Empty Field Values
body: <p>This has been going on for a while now, and I just can't seem to understand why. I've built a form that sends with PHPMailer and for whatever reason we continually get emails from the form with all blank field values. This is happening on multiple forms on different websites, all with the same setup. So it is not limited to this specific form.</p>
  
  <p>Blank form sends aside, the form does work properly when tested. If I fill it out and send a test, everything will come through fine. I'm not sure how these empty submissions are happening. The form has validation on most fields using javascript.</p>
  
  <p>Here is my HTML code:</p>
  
  <pre><code>                    &lt;div class="thank-you-message"&gt;&lt;/div&gt;
  
                  &lt;form id="contactForm" class="form-horizontal" action="" method="post"&gt;
  
  
                      &lt;!-- col 1 --&gt;
                      &lt;div class="form-l"&gt;
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;input type="text" name="first-name" id="first-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="first name" /&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;input type="text" name="last-name" id="last-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="last name" /&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="email address" /&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;input class="last" type="text" name="phone" id="phone" class="validate[required,custom[phone]]" placeholder="phone number" /&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
  
                      &lt;/div&gt;
                      &lt;!--../ col 1 --&gt;
                      &lt;!-- col 2 --&gt;          
                      &lt;div class="form-r"&gt;        
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;textarea rows="5" name="comments" id="comments" placeholder="message"&gt;&lt;/textarea&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
  
                      &lt;div class="control-group"&gt;
  
                          &lt;div class="controls"&gt;
                              &lt;button type="submit" class="submit"&gt;&lt;span&gt;send&lt;/span&gt;&lt;/button&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- /.control-group --&gt;
                      &lt;/div&gt;
                      &lt;!--../ col 1 --&gt;
  
                  &lt;/form&gt;
  </code></pre>
  
  <p>And here is the phpMailer code</p>
  
  <pre><code>&lt;?php
  
  // REQUIRE PHPMAILER CLASS
  // -----------------------------------------------------------------------------
  
  require_once("PHPMailerAutoload.php");
  $mail = new PHPMailer();
  
  
  // SET POST VARIABLES
  // -----------------------------------------------------------------------------
  
  $visitorEmail=filter_var($_POST['email']);
  $name=filter_var($_POST['first-name'].' '.$_POST['last-name']);
  $phone=filter_var($_POST['phone']);
  $comments=filter_var($_POST['comments']);
  
  
  
  // TO ADMIN EMAIL
  // -----------------------------------------------------------------------------
  
  // Mail Headers
  $mail-&gt;IsHTML(true); // Send as HTML
  
  $mail-&gt;AddReplyTo($visitorEmail, $name);
  $mail-&gt;From  = 'the@adminemail.com';
  $mail-&gt;FromName = 'The Admin &lt;the@adminemail.com&gt;';
  $mail-&gt;AddAddress("the@adminemail.com");
  
  // Mail Subject
  $mail-&gt;Subject  = "The Web Form";
  
  // Mail Body
  $mail-&gt;Body     = '&lt;html&gt;&lt;body&gt;
                                      &lt;img src="http://url.to/image.jpg" alt="Thank you for contacting us!" /&gt;
                                      &lt;p&gt;Someone has submitted your form.&lt;/p&gt;
                                      &lt;table rules="all" style="border-color: #666;" cellpadding="10"&gt;
                                      &lt;tr style="background: #eee;"&gt;&lt;td&gt;&lt;strong&gt;Sent From:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;The Web Form&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Name:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$name. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Email:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$visitorEmail. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Phone:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$phone. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Comments:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$comments. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;/table&gt;
                                      &lt;/body&gt;&lt;/html&gt;';
  
  $mail-&gt;Send(); // Send mail to admin
  $mail-&gt;ClearAddresses(); // Clear all addresses and attachments for next loop                           
  
  
  // TO VISITOR EMAIL
  // -----------------------------------------------------------------------------
  
  // Mail Headers
  $mail-&gt;IsHTML(true); // Send as HTML
  
  $mail-&gt;From  = 'the@adminemail.com';
  $mail-&gt;FromName = "The Admin &lt;the@adminemail.com&gt;";
  $mail-&gt;AddAddress($visitorEmail);
  
  // Mail Subject
  $mail-&gt;Subject  = "Thank you for contacting us";
  
  // Mail Body
  $mail-&gt;Body     = '&lt;html&gt;&lt;body&gt;
                                      &lt;img src="http://url.to/image.jpg" alt="Thank you for contacting us!" /&gt;
                                      &lt;p&gt;Hello,&lt;/p&gt; 
                                      &lt;p&gt;Thanks for contacting us. We have received your message and will get in touch with you shortly.&lt;/p&gt; 
                                      &lt;p&gt;A copy of the information you provided us with has been posted below:&lt;/p&gt;
                                      &lt;table rules="all" style="border-color: #666;" cellpadding="10"&gt;
                                      &lt;tr style="background: #eee;"&gt;&lt;td&gt;&lt;strong&gt;Name:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$name. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Email:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$visitorEmail. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Phone:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$phone. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;tr&gt;&lt;td&gt;&lt;strong&gt;Comments:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;' .$comments. '&lt;/td&gt;&lt;/tr&gt;
                                      &lt;/table&gt;
                                      &lt;/body&gt;&lt;/html&gt;';
  
  
  // THANK YOU MESSAGE
  // -----------------------------------------------------------------------------
  
  if($visitorEmail == '') {
      // If email field is blank, and validation doesn't work
      echo '&lt;h2&gt;Error&lt;/h2&gt;&lt;p&gt;An error has occurred. Please reload the page and try again. Thanks!&lt;/p&gt;';
  } else {
  
      if(!$mail-&gt;Send()) {
          // If mail fails to send
          echo '&lt;h2&gt;&lt;strong&gt;Message was not sent.&lt;/strong&gt;&lt;/h2&gt;';
          echo '&lt;p&gt;Mailer error: ' . $mail-&gt;ErrorInfo . '&lt;/p&gt;';
      } else {
          // Success
          echo '&lt;h2&gt;thanks for contacting us&lt;/h2&gt; &lt;p&gt;We will get back to you as soon as possible.&lt;/p&gt;
          &lt;p&gt;If you have any immediate questions please give us a call. &lt;/p&gt;';
      }
  
  }
  
  
  ?&gt;
  </code></pre>