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