doutuichan2681
doutuichan2681
2016-02-05 08:06

获取有关ajax成功的文本数据和json数据

I have developed following code which getting data from db.

    //get number of stores for zip code 
    $numberofstores = $resultstore = mysqli_query($conn, "SELECT store,address FROM geoinfo WHERE zipcode = '$search_text'");  
    //get values from db 

    $addressArray = Array();
    $storeArray = Array();

    while ($row = mysqli_fetch_array($resultstore, MYSQL_ASSOC)) {
    $storeArray[] =  $row['store'];  
    $addressArray[] = $row['address']; 
}
    //return data to the client 
    echo json_encode($addressArray); 
    echo json_encode($storeArray); 


  $total =  mysqli_num_rows($resultstore);
    if(mysqli_num_rows($resultstore) != 0) {
    echo "".$total." store/s found within this area";    

Here is my ajax call ,

  $(function () {

 $('#form_geocode').on('submit', function (e) {
          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'controller.php',
            dataType: 'text',
            data: $('#form_geocode').serialize(),
            success: function (data) { 
          for (var x = 0; x < data.length; x++) { 

             $("#location_results").html(data[x]+"<br>");


        }  

        }
          });

        });   

  });  

On success output is,

["226 N LARCHMONT BLVD, LOS ANGELES, CA","670 S WERN AVE, LOS ANGELES, CA","3201 W 6TH ST, LOS ANGELES, CA"]["RITE AID PHARMACY","RALPHS","WALGREENS?"]3 store/s found within this area      

My question is how can I extract 2 arrays into 2 arrays in js and store text separate variable.Please help.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongshungou7699 dongshungou7699 5年前

    Simple, combine the 3 elements into a larger array before JSON encoding;

    Change

    echo json_encode($addressArray); 
    echo json_encode($storeArray); 
    
    echo "".$total." store/s found within this area"; 
    

    to

    echo json_encode(array($addressArray, $storeArray, "".$total." store/s found within this area"));    
    
    点赞 评论 复制链接分享