如何从php返回整个值作为json格式?

时间:2014-01-16 15:07:26

标签: javascript php jquery ajax json

我们有一个名为getBuilds.php的php文件。当您运行此文件时,它将查询数据库并返回以json格式编码的整个值。以下是样本数据:

   [
    {
     "BuildingDisplay":"132 Mitchell Street - 132 Mitchell St., SW",
    "BuildingID":"B610012",
    "Address":"132 Mitchell St., SW",
    "City":"Chicago",
    "District":"Central",
    "Location":"B610012 132 Mitchell Street ",
    "State":"IL",
    "StreetName":"132 Mitchell St., SW",
    "Zip":"30303",
    "X":2227970.4292704,
    "Y":1364292.9044986
   },
   {
    "BuildingDisplay":"34 Peachtree Street - 34 Peachtree St.",
    "BuildingID":"B630012",
    "Address":"34 Peachtree St.",
    "City":"Chicago",
    "District":"Central",
    "Location":"B630012 34 Peachtree Street",
    "State":"IL",
    "StreetName":"34 Peachtree St.",
    "Zip":"30303","X":2228810.0213674,
    "Y":1365970.5523757},
    Etc
    etc
  ]

然后我使用以下jQuery / Ajax以json格式返回BuildingID,然后使用BuildingID的值填充下拉列表:

 function getBuildings() {
    $.ajax({
    url: 'services/getBuilds.php',
    dataType: 'json'
    })
    .done(function(buildingInfo) {
    $(buildingInfo).each(function(i, building) {
    $('<option>').val(building.BuildingID).text(building.BuildingDisplay).appendTo( $('#buildingID') );
          })
    });
    }

这很好但我们不仅要求提取建筑物ID,而且还要提取PHP文件中的所有值,并将它们全部作为json格式返回。

这样,我们仍然可以使用BuildingID填充下拉列表,但将其余部分填充为隐藏值。

我知道如何将json数据填充为隐藏的表单值,但我不知道如何以json格式返回所有数据行。

任何想法都将不胜感激。

如果需要,我将非常乐意澄清。

提前多多感谢

1 个答案:

答案 0 :(得分:1)

你几乎拥有你需要的东西。只需使用jQuery创建一些隐藏的输入。此外,请注意此处的代码更加简化并针对性能进行了优化,因为.each()执行速度比原始for()循环慢约50%。

function getBuildings() {
    $.ajax({
        url: 'services/getBuilds.php',
        dataType: 'json'
    }).done(function (buildingInfo) {

        // get number of items in array given by PHP
        var buildings_count = buildingInfo.length;

        // start an array to hold re-arranged buildings array
        var newBuildingInfo = [];

        // loop buildings
        for (var i = 0; i < buildings_count; i += 1) {

            // append an <option> tag to your <select>
            $('#buildingID').append('<option value="' + buildingInfo[i].BuildingID + '">' + buildingInfo[i].BuildingDisplay + '</option>');

            // set the key value to BuildingID so that it is easily referenced later
            newBuildingInfo['' + buildingInfo[i].BuildingID + ''] = buildingInfo[i];

            // using single-quotes in newBuildingInfo['' + buildingInfo[i].BuildingID+ ''] is important
            // if a BuildingID is 000432, notice the starting zeros, then things will get funky

        }

        // Listen for the value of the <select> to change
        $('#buildingID').on('change', function () {

            // This code assumes you already have these hidden inputs available on the form before this Javascript is ever used
            // Set the value of the hidden fields based on the <select>'s ID choosing the corret array element
            $('input[name="Address"]').val(newBuildingInfo[''+$(this).val()+''].Address);
            $('input[name="City"]').val(newBuildingInfo[''+$(this).val()+''].City);
            $('input[name="District"]').val(newBuildingInfo[''+$(this).val()+''].District);
            // etc...

        });
    });
}