title: PHP json_encode for JQuery
body: <p>I'm working on a method in my class that outputs error messages for both ajax and regular post request. The PHP part works fine, but the json part doesn't seem to. Here is my method:</p>
  
  <pre><code>public $formError = false;
  public $phpErrors = '';
  public $jsonErrors = array();
  
  // ------------------------------------------------------------
  // ERROR PROCESSING
  // ------------------------------------------------------------
  private function responseMessage($bool, $msg) {
      $return['error'] = $bool;
      $return['msg'] = $msg;
      if (isset($_POST['plAjax']) &amp;&amp; $_POST['plAjax'] == true) {
          $this-&gt;jsonErrors[] = $return;
      } else {
          foreach ((array) $msg as $key =&gt; $value) {
              $this-&gt;phpErrors .= $msg;
          }
      }
      $this-&gt;formError = true;
  }
  </code></pre>
  
  <p>I think, the problem is that no matter if it is just a single error message or multiple, the json object is always wrapped with square brackets. I couldn't find anything online that would show an example. The messages look like this:</p>
  
  <p>SINGLE ERROR:</p>
  
  <p>[{"error":true,"msg":"Error message 1 ..."}]</p>
  
  <p>MULTIPLE ERRORS:</p>
  
  <p>[{"error":true,"msg":"Error message 1 ..."},{"error":true,"msg":"Error message 2 ..."}]</p>
  
  <p>Firebug shows 200 OK but my JQuery does not output any messages:</p>
  
  <pre><code>$.ajax({
      type: 'POST',
      url: plSubmitUrl,
      data: plFormData,
      dataType: 'json',
      cache: false,
      timeout: 10000,
      success: function (data) {
          if (data.error === true) {
  
              // display error message
              plResponse(data.msg, true);
  
              ...
          } else if (data.error === false) {
  
              // display success message
              plResponse(data.msg, true);
  
              ...
          }
      },
  </code></pre>
  
  <p>When I was showing only one message at a time, the JQuery was working fine.</p>
  
  <p>Any help would be great. Thanks</p>