I have two dropdown list which is question and answer.
At first the answer dropdown list is empty, after the user choose a question, then it will pass the question_id to controller to run a function to get the answer. After the controller get the result, it will pass to the correspond view. Now how can I pass the result to the index view?
the index view:
$("#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
}
});
});`
the controller:
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;
}
the checkanswer.phtml:
foreach ($this->answer as $key => $value)
{
echo '<option value="'.trim($value['id_answer']).'">'. trim($value['answer_text']) .'</option>';
}
The content that should be displayed in
checkanswer.phtmlwill be affected to your javascript varresponse. So if you want to display this in your page, you have to make something like this :