title: How do I return a proper success/error message for JQuery .ajax() using PHP?
body: <p>I keep getting the error alert. There is nothing wrong with the MYSQL part, the query gets executed and I can see the email addresses in the db.</p>
  
  <p><strong>The client side:</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(function() {
      $("form#subsribe_form").submit(function() {
        var email = $("#email").val();
  
        $.ajax({
          url: "subscribe.php",
          type: "POST",
          data: {email: email},
          dataType: "json",
          success: function() {
            alert("Thank you for subscribing!");
          },
          error: function() {
            alert("There was an error. Try again please!");
          }
        });
        return false;
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>The server side:</strong></p>
  
  <pre><code>&lt;?php 
  $user="username";
  $password="password";
  $database="database";
  
  mysql_connect(localhost,$user,$password);
  mysql_select_db($database) or die( "Unable to select database");
  
  $senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
  
  if($senderEmail != "")
      $query = "INSERT INTO participants(col1 , col2) VALUES (CURDATE(),'".$senderEmail."')";
  mysql_query($query);
  mysql_close();
  
  $response_array['status'] = 'success';    
  
  echo json_encode($response_array);
  ?&gt;
  </code></pre>