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

时间:2016-02-05 08:06:31

标签: php jquery json ajax

我开发了以下从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";    

这是我的ajax电话,

  $(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>");


        }  

        }
          });

        });   

  });  

成功输出是,

["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      

我的问题是如何在js中将2个数组提取为2个数组并将文本存储为单独的变量。请帮助。

1 个答案:

答案 0 :(得分:1)

简单,在JSON编码之前将3个元素组合成一个更大的数组;

更改

echo json_encode($addressArray); 
echo json_encode($storeArray); 

echo "".$total." store/s found within this area"; 

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