title: AJAX/JQuery and PHP to send prompt without reload for a contact form
body: <p>Full website: <a href="http://adamginther.com" rel="nofollow">http://adamginther.com</a></p>
  
  <p>I've used two separate tutorials to build this, one for the PHP and one for the AJAX. My goal is to build a contact form that will check for errors. If there are no errors then it will prompt users with a message saying that the message has been sent without refreshing the page.</p>
  
  <p>When the website is run locally it does not display the errors until the button is pressed, when run on the server the errors are displayed on entering the page. When the contact button is pressed it loads PHP and the page goes blank.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;form action="contact.php" method="post"&gt;
  &lt;label name="firstName"&gt;Name: &lt;/label&gt;
  &lt;input type="text" name="firstName" id="firstName"&gt;
  &lt;label class="error" for="firstName" id="name_error"&gt;I need your name.&lt;/label&gt;
  &lt;br id="namebreak"&gt;
  &lt;br&gt;
  &lt;label name="email" for="email" id="email_label"&gt;E-mail Address: &lt;/label&gt;
  &lt;input type="text" name="email" id="email"&gt;
  &lt;label class="error" for="firstName" id="name_error"&gt;I need your e-mail.&lt;/label&gt;
  &lt;br id="emailbreak"&gt;
  &lt;br&gt;
  &lt;label name="message"&gt;Message: &lt;/label&gt;
  &lt;textarea name="message" id="message"&gt;&lt;/textarea&gt;
  &lt;label class="error" for="firstName" id="name_error"&gt;I need your message.&lt;/label&gt;
  &lt;br id="messagebreak"&gt;
  &lt;br&gt;
  &lt;input type="submit" value="Say Hello!" id="contactbutton" class="button"&gt;
  </code></pre>
  
  <p>JavaScript</p>
  
  <pre><code>$(function () {
      $('.error').hide();
      $(".button").click(function () {
  
          $('.error').hide();
          var name = $("input#firstName").val();
          if (name == "") {
              $("label#name_error").show();
              $("input#name").focus();
              $("#namebreak").hide();
              return false;
          }
          var email = $("input#email").val();
          if (email == "") {
              $("label#email_error").show();
              $("input#email").focus();
              $("#emailbreak").hide();
              return false;
          }
          var message = $("input#message").val();
          if (message == "") {
              $("label#message_error").show();
              $("input#message").focus();
              $("#messagebreak").hide();
              return false;
          }
  
          var dataString = 'name=' + name + '&amp;email=' + email + '&amp;phone=' + phone;
  
          $.ajax({
              type: "POST",
              url: "contact.php",
              data: dataString,
              success: function () {
                  $('#contactme').html("&lt;div id='message'&gt;&lt;/div&gt;");
                  $('#message').html("&lt;p&gt;Contact form submitted.&lt;/p&gt;")
                      .append("&lt;p&gt;I will get back to you shortly.&lt;/p&gt;")
                      .hide()
                      .fadeIn(1500, function () {
                      $('#message').append("&lt;img id='checkmark' src='images/check.png' /&gt;");
                  });
              }
          });
          return false;
  
      });
  });
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  $field_firstName = $_POST['firstName'];
  $field_email = $_POST['email'];
  $field_message = $_POST['message'];
  
  $mail_to = 'gintherthegreat@gmail.com';
  $subject = 'AdamGinther.com message from '.$field_firstName;
  
  $body_message = 'From: '.$field_firstName."\n";
  $body_message .= 'E-mail: ' .$field_email."\n";
  $body_message .= 'Message: '.$field_message;
  
  $headers = 'From: '.$field_email."\r\n";
  $headers .= 'Reply-To: '.$field_email."\r\n";
  
  $mail_status = mail($mail_to, $subject, $body_message, $headers);
  
  if ($mail_status) { ?&gt;
      &lt;script language="javascript" type="text/javascript"&gt;
      $('#panel').show();
      $('#output-inside').text('Thank you ' + firstName + ', I will get back to you as soon as I can.');
      &lt;/script&gt;
  &lt;?php
  }
  else { ?&gt;
      &lt;script language="javascript" type="text/javascript"&gt;
          $('#panel').show();
      $('#output-inside').text('I am sorry ' + firstName + ', but there was a problem processing your request. I can be contacted by e-mail at GintherTheGreat@Gmail.com');   &lt;/script&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>