2014-07-06 3 views
0

zf2를 사용 중입니다. ajax 호출을 사용하여 두 번째 드롭 다운을로드하려고합니다. 나는 다음 코드를 시도했다. 하드 코딩 된 값을 얻을 수 있습니다. 하지만 나는 배열에 데이터베이스 값을 추가하고 아약스를 사용하여 드롭 다운 값을로드하는 방법을 모른다. PHTML에서zf2를 사용하여 ajax + json을 수행하는 방법은 무엇입니까?

아약스 :

<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

이것은 내 컨트롤러에서 수행 한 것입니다. 마지막으로 코딩을 마쳤습니다. 이

<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 쿼리를 예전처럼

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; 
} 

이와 아약스이다. 도와 주셔서 감사합니다 모두 :

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> 
관련 문제