doutuichan2681 2016-02-05 08:06
浏览 43
已采纳

获取有关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 2016-02-05 08:11
    关注

    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"));    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计