title: Returning multiple JSON objects from PHP to jQuery via AJAX
body: <p>This question has been asked already but it is not solving my specific query. What I am trying to achieve is whenever a user inputs the pincode, via AJAX I am sending the pincode to PHP and returning the results with that pincode. If a single area is mapped to a particular pincode then my code works. But if there are multiple areas with same pincode then my code breaks. I have tried multiple ways. I have even tried this: <a href="https://diffiebosman.wordpress.com/2012/09/30/how-to-return-multiple-json-objects-from-php-to-jquery-with-ajax/" rel="nofollow noreferrer">Link</a></p>
  <p>Here is my code</p>
  <pre><code>$('.input-pc').on('keyup change', function(){
      PincodeId = $(this).attr('id');
      PincodeVal = $(&quot;#&quot; + PincodeId).val();
      checkPincodeLength = PincodeVal.length;
      if(checkPincodeLength == 6){
          $('#' + PincodeId).next().next('.invalid-feedback').html('');
          $.ajax({
              url: &quot;../../auth/regrequest.php&quot;,
              type: &quot;POST&quot;,
              data: 'current_pincode='+PincodeVal,
              dataType: 'JSON',
              cache: false,
              success: function(data){
                  console.log('pc: '+data);
                  $.each(data, function(index, val) {
                     console.log(val);
                 })
                                     
              }
          });
      }else{
          $('#' + PincodeId).next().next('.invalid-feedback').html('&lt;span&gt;&lt;i class=&quot;fa fa-exclamation-circle&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&amp;nbsp;Pincode must be of 6 digits&lt;/span&gt;');
      }
  });
  </code></pre>
  <p>I have even tried removing the dataType and parsed the data using JSON.parse. This too did not work.</p>
  <p>My PHP code looks like this</p>
  <pre><code>if(isset($_POST['current_pincode']) &amp;&amp; !empty($_POST['current_pincode'])){
      $current_pincode = $_POST['current_pincode'];
          $query26 = $dbc-&gt;prepare(&quot;SELECT a.area_id, a.city_id, a.state_id, b.state_name, c.city_name, d.area_name FROM pincodes a, states b, cities c, areas d WHERE a.pincode_code = ? AND a.state_id = b.state_id AND a.city_id = c.city_id AND a.area_id = d.area_id GROUP BY area_id ORDER BY area_id&quot;);
          $query26-&gt;bindParam(1, $current_pincode);
          $query26-&gt;execute();
          $citystate = '';
          $city_id = '';
          //$citystatearray = array(
             // 'CityState' =&gt; array(),
          //);
          //$i = 0;
          //while ($getcitystate = $query26-&gt;fetch(PDO::FETCH_ASSOC)){
           //   $citystatearray['CityState'][$i++] = $getcitystate;
           //   echo json_encode($citystatearray); 
         // }
          foreach($query26 AS $getcitystate){
              
              // $state_id = $getcitystate['state_id'];
              // $state_name = $getcitystate['state_name'];
              // $city_id = $getcitystate['city_id'];
              // $city_name = $getcitystate['city_name'];
              // $area_name = $getcitystate['area_name'];
              // $rowCount = $query26-&gt;rowCount();
              
              // //$citystate .= $area_name.',';
              // //$pct = $getcitystate['pincodetime'];if(count($area_name) == 1){echo 'only one';}else{echo 'more than 1';}
               if($rowCount == 1){
              //     $citystate = 
              //                 array(
              //                     'current_pincode' =&gt; $current_pincode,
              //                     'state_id' =&gt; $state_id,
              //                     'state_name' =&gt; $state_name,
              //                     'city_id' =&gt; $city_id,
              //                     'city_name' =&gt; $city_name,
              //                     'area_name' =&gt; $area_name,
              //                     'rowcount' =&gt; $rowCount
              //                  );
               }else{
                  
                  $citystate .= 
                              array(
                                  
                                                  'current_pincode' =&gt; $current_pincode,
                                                  'state_id' =&gt; $state_id,
                                                  'state_name' =&gt; $state_name,
                                                  'city_id' =&gt; $city_id,
                                                  'city_name' =&gt; $city_name,
                                                  'area_name' =&gt; array('area_name_inner' =&gt; $area_name),
                                                  'area_name' =&gt; $area_name,
                                                  'rowcount' =&gt; $rowCount
                                                  
                          );
               }
              //echo $citystate;
              
             
          }echo json_encode(
              array(
                  'citystate' =&gt; $citystate
              )); //echo json_encode(
          //     array(
          //         'citystate' =&gt; $citystate
          //     ));//echo $citystate;
          //echo $city_id;
         // echo $citystate1 = $city_id.','.$rowCount.','.$citystate;
  }
  </code></pre>
  <p>I have not removed the comments to show the different ways i tried to output the data but in vain. Any help would be highly appreciated. Thanks!</p>