title: With this combo of jQuery, AJAX and PHP, how do I catch errors coming from the PHP?
body: <p>I handle it if the PHP returns successfully, but as far as errors go I'm a little confused as to what I should be doing.</p>
  
  <p>What I was thinking of doing since the following code for <code>error:</code> doesn't work, was in my PHP setting the mail function equal to a variable in order to save the result of the function. Then in my jQuery, testing the result of the variable to find whether or not it's true.</p>
  
  <p>However this would all occur under <code>success:</code> when it seems like clearly error should be handling this.</p>
  
  <p>How do I have it so <code>success:</code> handles the successes correctly and displays a message while <code>error:</code> will handle the errors by returning the errors then I'll be able to display them to the user.</p>
  
  <p>Also bonus points if you can tell me if my <code>.html()</code> function is done well? It looks messy formatting-wise, but I can't seem to figure a nicer way to do it.</p>
  
  <h1>jQuery:</h1>
  
  <pre><code>// Following section submits form without refreshing page
  var dataString = "name=" + nameVal + "&amp;email=" + emailVal + "&amp;message=" + messageVal;  
  $.ajax({  
      type: "POST",  
      url: "mail.php",  
      data: dataString,  
      success: function() { 
          $(".contact-form").hide();
          $(".alt-contact").hide();
      // Inserts divs making up the success message for the form submission
          $(".contact-form").html("&lt;div class='success-message'&gt;&lt;div class='success-image'&gt;&lt;/div&gt;&lt;div class='success-title'&gt;Success! The message has been sent!&lt;/div&gt;&lt;div class='success-body'&gt;I'll get back to you right away.&lt;/div&gt;&lt;/div&gt;");
          $(".contact-form").fadeIn(500);
      }
      error: function() {
          $(".contact-form").hide();
          $(".alt-contact").hide();
          // Inserts divs making up the success message for the form submission
          $(".contact-form").html("&lt;div class='error-message'&gt;&lt;div class='error-image'&gt;&lt;/div&gt;&lt;div class='error-title'&gt;Success! The message has been sent!&lt;/div&gt;&lt;div class='error-body'&gt;I'll get back to you right away.&lt;/div&gt;&lt;/div&gt;");
          $(".contact-form").fadeIn(500);
      }
  });
  return false;
  </code></pre>
  
  <h1>PHP:</h1>
  
  <pre><code>&lt;?php
  
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];
  
      $recipient = 'me@christianselig.com';
      $subject = 'Message From Website';
      $body = '&lt;b&gt;From:&lt;/b&gt; ' . $name . '\n\n' . $message;
      $headers = 'From: ' . $email . '\r\n';
      $headers .= 'Content-type: text/html; charset=UTF-8' . '\r\n';
  
      mail($recipient, $subject, $body, $headers);
  
  ?&gt;
  </code></pre>