title: PHP, MySQL, jQuery, AJAX: json data returns correct response but frontend returns error
body: <p>I have a user registration form. I am doing server side validation on the fly via AJAX. The quick summary of my problem is that upon validating 2 fields, I get error for the second field validation. If I comment first field, then the 2nd field does not show any error. It has this weird behavior. More details below:</p>
  
  <p>The HTML, JS and Php code are below:</p>
  
  <p><strong>HTML FORM:</strong></p>
  
  <pre><code>&lt;form id="SignupForm" action=""&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Free Signup&lt;/legend&gt;
          &lt;label for="username"&gt;Username&lt;/label&gt;
          &lt;input name="username" type="text" id="username" /&gt;&lt;span id="status_username"&gt;&lt;/span&gt;
          &lt;br /&gt;
          &lt;label for="email"&gt;Email&lt;/label&gt;
          &lt;input name="email" type="text" id="email" /&gt;&lt;span id="status_email"&gt;&lt;/span&gt;
          &lt;br /&gt;
          &lt;label for="confirm_email"&gt;Confirm Email&lt;/label&gt;
          &lt;input name="confirm_email" type="text" id="confirm_email" /&gt;&lt;span id="status_confirm_email"&gt;&lt;/span&gt;
          &lt;br /&gt;
      &lt;/fieldset&gt;
      &lt;p&gt;
          &lt;input id="sbt" type="button" value="Submit form" /&gt;
      &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>&lt;script type="text/javascript" src="js/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
  
    $("#email").blur(function() {
      var email = $("#email").val();
      var msgbox2 = $("#status_email");
      if (email.length &gt; 3) {
        $.ajax({
          type: 'POST',
          url: 'check_ajax2.php',
          data: "email=" + email,
          dataType: 'json',
          cache: false,
          success: function(data) {
            if (data.success == 'y') {
              alert('Available');
            } else {
              alert('Not Available');
            }
          }
        });
      }
      return false;
    });
  
    $("#confirm_email").blur(function() {
      var confirm_email = $("#confirm_email").val();
      var email = $("#email").val();
      var msgbox3 = $("#status_confirm_email");
  
      if (confirm_email.length &gt; 3) {
        $.ajax({
          type: 'POST',
          url: 'check_ajax2.php',
          data: 'confirm_email=' + confirm_email + '&amp;email=' + email,
          dataType: 'json',
          cache: false,
          success: function(data) {
            if (data.success == 'y') {
              alert('Available');
            } else {
              alert('Not Available');
            }
          },
          error: function(data) {
            alert('Some error');
          }
        });
      }
      return false;
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>PHP code:</strong></p>
  
  <pre><code>&lt;?php //check_ajax2.php
  
  
  if(isset($_POST['email']))
  {
      $email = $_POST['email'];
  
  
      $res = mysql_query("SELECT uid FROM members WHERE email = '$email' ");
      $i_exists = mysql_num_rows($res);
  
      if( 0 == $i_exists )
      {
          $success = 'y';
          $msg_email = 'Email available';
      }
      else
      {
          $success = 'n';
          $msg_email = 'Email is already in use.&lt;/font&gt;';
      }
  
      print json_encode(array('success' =&gt; $success, 'msg_email' =&gt; $msg_email)); 
  }
  
  if(isset($_POST['confirm_email']))
  {
      $confirm_email = $_POST['confirm_email'];
      $email = ( isset($_POST['email']) &amp;&amp; trim($_POST['email']) != '' ? $_POST['email'] : '' );
  
  
  
      $res = mysql_query("SELECT uid FROM members WHERE email = '$confirm_email' ");
      $i_exists = mysql_num_rows($res);
  
  
      if( 0 == $i_exists ) 
      {
          if( isset($email) &amp;&amp; isset($confirm_email) &amp;&amp;  $email == $confirm_email )
          {
              $success = 'y';
              $msg_confirm_email = 'Email available and match';
          }
          else
          {
              $success = 'n';
              $msg_confirm_email = 'Email and Confirm Email do NOT match.';
          }       
      }
      else
      {
          $success = 'n';
          $msg_confirm_email = 'Email already exists.';
      }
  
      print json_encode(array('success' =&gt; $success, 'msg_confirm_email' =&gt; $msg_confirm_email)); 
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>THE PROBLEM:</strong></p>
  
  <p>As long as I am validating the <code>$_POST['email']</code> as well as <code>$_POST['confirm_email']</code> in the check_ajax2.php file, the validation for confirm_email field always returns an error. With my limited knowledge of Firebug, however, I did find out that the following were the responses when I entered email and confirm_email in the fields:</p>
  
  <p><strong>RESPONSE 1:</strong>
  {"success":"y","msg_email":"Email available"}</p>
  
  <p><strong>RESPONSE 2:</strong>
  {"success":"y","msg_email":"Email available"}{"success":"n","msg_confirm_email":"Email and Confirm Email do NOT match."}</p>
  
  <p>Although the RESPONSE 2 shows that we are receiving the correct message via msg_confirm_email, in the front end, the alert 'Some error' is popping up (I have enabled the alert for debugging). I have spent 48 hours trying to change every part of the code wherever possible, but with only little success. What is weird about this is that if I comment the validation for <code>$_POST['email']</code> field completely, then the validation for <code>$_POST['confirm_email']</code> field is displaying correctly without any errors. If I enable it back, it is validating email field correctly, but when it reaches the point of validating confirm_email field, it is again showing me the error. </p>
  
  <p>I have also tried renaming success variable in check_ajax2.php page to other different names for both <code>$_POST['email']</code> and <code>$_POST['confirm_email']</code> but no success. I will be adding more fields in the form and validating within the check_ajax2.php page. So I am not planning on using different ajax pages for validating each of those fields (and I don't think it's smart to do it that way). I am not a jquery or AJAX guru, so all help in resolving this issue is highly appreciated.</p>
  
  <p>Thank you in advance.</p>