我有两个下拉列表,即问答。起初答案下拉列表是空的,用户选择一个问题后,它会将 question_id 传递给控制器运行一个函数来获取答案。控制器得到结果后,会传递给对应的视图。现在如何将结果传递给索引视图?
索引视图:
$("#id_question").change(function() {
var data = $("#id_question").val();
var dataToSend = {question: data}
var href= '<?php echo $this->baseUrl('admin/comment/checkanswer'); ?>';
$.ajax({ type: "POST",
url: href,
data: dataToSend,
success: function(response){
//do what u wana do
}
});
});`
控制器:
public function checkanswerAction()
{
$this->_helper->layout->disableLayout();
$question_id = $this->getRequest()->getParam('question');
$answer_model = new Admin_Model_DbTable_Answer();
$answer = $answer_model->getAnswersByQuestionId($question_id);
$this->view->answer = $answer;
}
checkanswer.phtml:
foreach ($this->answer as $key => $value)
{
echo '<option value="'.trim($value['id_answer']).'">'. trim($value['answer_text']) .'</option>';
}