使用getJSON ajax函数从json格式化的PHP中获取值?

时间:2013-03-30 09:53:11

标签: php javascript html json

我有这个javascript函数代码来获取StationInfoGenerator.php.But中的值,当我想获取乘客的值时(例如from,to,arrivalTime,waitingTime), 我无法将这些值放在html页面中相应的div标签上。

function sta_callStation() {
    $('#noOfPassengers, #infoOfPassengers, #distType,#distParams').empty();
    $.getJSON('StationInfoGenerator.php', function (station) {
        $.each(station, function (sta_key, sta_value) {
            if (sta_key == "numberOfPassengers") {
                $('#noOfPassengers').append("<strong>Number Of Passengers:       
     </strong>" + sta_value);
            }
            if (sta_key == "passengers") {
                $('#infoOfPassengers').append("<strong>Passengers Information:</strong>" + '<br/>');
                $.each(sta_key.passengers.parameters, function (i, parameters) {
                    $.each(parameters, function (par_key, par_value) {
                        $('#infoOfPassengers').append(par_key + ': ' + par_value + '  ' + '<br/>');
                    });
                });
            }
            if (sta_key == "distType") {
                $('#distType').append("<strong>Dağılım Tipi:</strong>" + sta_value);
            }
        });
    });
}

这是html代码部分。在这部分当我选择一些车站时,我必须得到乘客人数,乘客信息,分配类型和参数。现在我可以从php获得numberOfPassengers。在php中,所有值都是随机制作。

<select name="selectStation" class="span12" onchange="sta_callStation();">
    <option value="noStation">Bir istasyon seçiniz.</option>
    <option value="station1">Station 1</option>
    <option value="station2">Station 2</option>
    <option value="station3">Station 3</option>
    <option value="station4">Station 4</option>
    <option value="station5">Station 5</option>
  </select>


      <div id="noOfPassengers"><strong>Number Of Passengers:</strong></div>
      <p><i class="icon-user"></i>  <i class="icon-user"></i>  <i class="icon-user"> 
  </i></p>
      <div id="infoOfPassengers"></div>
      <div id="distType"></div>
      <div id="distParams"></div>

Php jsonSerialize部分:

 public function jsonSerialize()
{
    $data = array();
    $data['from'] = $this->from;
    $data['to'] = $this->to;
    $data['arrivalTime'] = $this->arrivalTime;
    $data['waitingTime'] = $this->waitingTime;
    return $data;
}

网页中的输出:

Number Of Passengers:5


Passengers Information:

如何显示乘客信息? 感谢。

2 个答案:

答案 0 :(得分:0)

public function jsonSerialize()
{
    $data = array();
    $data['from'] = $this->from;
    $data['to'] = $this->to;
    $data['arrivalTime'] = $this->arrivalTime;
    $data['waitingTime'] = $this->waitingTime;
    return json_encode($data);
}

答案 1 :(得分:0)

function sta_callStation()
  {
    var distName;
    $('#noOfPassengers, #infoOfPassengers, #distType,#distParams').empty();

    $.getJSON('StationInfoGenerator.php', function(station){

        $.each(station, function(sta_key, sta_value) {

        if(sta_key=="numberOfPassengers") {

        $('#noOfPassengers').append("<strong>Number Of Passengers: 
 </strong>"+sta_value);

           }

        if(sta_key=="passengers")
        {

          $('#infoOfPassengers').append("<strong>Passengers Information:     
     </strong>"+'<br/>');

          $.each(station.passengers, function(i,passenger) {

           $.each(passenger, function(par_key, par_value) {


              $('#infoOfPassengers').append(par_key+': '+par_value+'  '+'<br/>');
         });
           $('#infoOfPassengers').append('<hr />');
        });

        }

        if(sta_key=="distType") 
        {

          if(sta_value=="1"){
            distName="Uniform";
          }
          if(sta_value=="2"){
            distName="Normal";
          }
          if(sta_value=="3"){
            distName="Üstel";
          }
          if(sta_value=="4"){
            distName="Geometric";
          }

        $('#distType').append("<strong>Distribution Type:</strong>"+sta_value+"<br/>"+"<strong>Distribution Name:</strong>"+distName);


      }
        if(sta_key=="distParams") 
        {

         $('#distParams').append("<strong>Distribution Parameters:</strong>"+'<br/>');



            $.each(station.distParams, function(params_key, params_value) {


              $('#distParams').append(params_key+': '+params_value+'  '+'<br/>');
              //$('#distParams').append('<hr />');
         });





        }
      });

  });
  }