2014-07-06 33 views
0

我正在使用zf2。我想通过使用ajax调用来加载我的第二个下拉列表。我尝试了下面的代码。我可以得到硬编码的值。但我不知道如何将数据库值添加到数组中并使用ajax将该值加载到下拉列表中。如何使用zf2做ajax + json?

阿贾克斯PHTML:

<script type="text/javascript"> 
$(document).ready(function() { 

$("#projectname").change(function (event) { 

    var projectname = $(this).val(); 
    var projectkey = projectname.split(" - "); 
    var projectname = {textData:projectkey[1]}; 


    //The post using ajax 
    $.ajax({ 
      type:"POST", 
      // URL :/name of the controller for the site/name of the action to be       
      //             executed 
      url:'<?php echo $this->url('userstory', array('action'=>'answer')); ?>', 
      data:projectname, 
      success: function(data){ 

       //code to load data to the dropdown 

      }, 
      error:function(){alert("Failure!!");} 


      }); 

    }); 
    }); 

</script> 

控制器动作:

public function answerAction() { 

    // ead the data sent from the site 
    $key = $_POST ['textData']; 

    // o something with the data 
    $data= $this->getProjectTable()->getkeyproject($key); 
    $projectid = $data->id; 

    $projectusers[] = $this->getRoleTable()->fetchRoles($projectid); 
    // eturn a Json object containing the data 

    $result = new JsonModel (array (
      'projectusers' => $projectusers 
    )); 
    return $result; 
} 

DB查询:

public function fetchRoles($id) { 
    $resultSet = $this->tableGateway->select (array (
      'projectid' => $id 
    )); 

    return $resultSet; 

} 

回答

0

这就是我在我的控制器中所做的。最终完成编码。

public function answerAction() { 

    // ead the data sent from the site 
    $key = $_POST ['textData']; 

    // o something with the data 
    $data= $this->getProjectTable()->getkeyproject($key); 
    $projectid = $data->id; 
    $i=0; 
    $text[0] = $data->id. "successfully processed"; 
    $projectusers = $this->getRoleTable()->fetchRoles($projectid); 

    foreach ($projectusers as $projectusers) : 
    $users[$i][0] = $projectusers->username; 
    $users[$i][1] = $projectusers->id; 
    $i++; 
    // eturn a Json object containing the data 
    endforeach; 
    $result = new JsonModel (array (
      'users' => $users,'count'=>$i 
    )); 
    return $result; 
} 

和Ajax是这样

<script type="text/javascript"> 
$(document).ready(function() { 

$("#projectname").change(function (event) { 

    var projectname = $(this).val(); 
    var projectkey = projectname.split(" - "); 
    var projectname = {textData:projectkey[1]}; 


    //The post using ajax 
    $.ajax({ 
      type:"POST", 
      // URL :/name of the controller for the site/name of the action to be       
      //             executed 
      url:'<?php echo $this->url('userstory', array('action'=>'answer')); ?>', 
      data:projectname, 
      success: function(data){ 
       // alert(data.users[0][0]+" - " + data.users[0][1]); 

       var count= data.count; 
       alert(count); 
       $('#myDropDown').empty(); 
       for(var i=0;i<count;i++){ 

        $('#myDropDown').append($('<option></option>').attr('value', data.users[i][1]).text(data.users[i][0])); 

       } 

      }, 
      error:function(){alert("Failure!!");} 


      }); 

}); 
    }); 

</script> 

使用相同的ZF2查询来访问数据库。感谢大家的帮助:)

0

您的JSON对象new JsonModel (array ( 'projectusers' => $projectusers ) JSON对象变成了这个样子格式Click here for Demo

var projectkey = []; 

     projectkey = projectname.split(" - "); 
     var projectname = { "textData" : "+projectkey[1]+" }; 

     $.ajax({ 
     type:"POST", 
     url  : "url.action", 
     data : projectname, 
     success : function(data){ 

      $.each(data.projectusers,function(key,value){ 
       $('#divid').append("<option value="+key+">"+value+"</option>"); 
      }); 
      }); 
     }); 



    <select id="divid"></select>