如何在更改<select>字段的选项时在cakephp中检索数据?</select>

时间:2012-09-11 10:33:54

标签: jquery html cakephp-2.0

我在cakephp中真的很新。我有两个名为'问题''答案'的控制器。在答案视图中,我有一个admin_add.ctp文件,其中包含一个显示问题列表的选择框。但问题是当有人选择问题时,它将显示不同输入字段中的所有答案,具体取决于所选的question_id,尽管我的admin_add.ctp包含单个输入字段。因此,当有人进行更改时,必须动态地将字段添加到admin_add ctp。我的答案表包含一个字段'question_id'作为foreign_key。这是我的admin_add.ctp文件..

<?php echo $this->
    Html->script(array('answers_add','jquery-ui-1.8.18.custom.min'), array('inline'
    => false)); ?>
    <div class="answers form">
        <?php echo $this->
            Form->create('Answer');?>
            <fieldset>
                <legend>
                    <?php echo __( 'Admin Add Answer'); ?>
                </legend>
                <?php echo $this->
                    Form->input('question_id', array('empty'=>'Please select the question'));?>
                    <span class="labelToAnswerBox">
                        Add your answers
                    </span>
                    <ul id="allAnswerHolder">
                        <li class="eachAnswer">
                            <?php echo $this->
                                Form->input('answer', array('id' => false, 'class' => 'answerInput', 'name'
                                => 'data[Answer][answer][]','div' => false, 'label' => false, 'after' =>
                                '
                                <input type="button" value="X" class="removeAnswer">
                                ', 'before' => '
                                <span class="ui-icon ui-icon-arrowthick-2-n-s">
                                </span>
                                ')); ?>
                        </li>
                    </ul>
                    <span class="addAnswer">
                        Add
                    </span>
                    <?php echo $this->
                        Form->input('Status', array('type'=>'hidden', 'id'=>'status')); echo $this->Form->input('Status1',
                        array('type'=>'hidden', 'id'=>'status1')); ?>
            </fieldset>
            <?php echo $this->
                Form->end(__('Submit'));?>
    </div>
    <?php echo $this->
        element('left');?>  

任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试如下:

使用AJAX请求为change()框写select方法:

$('select#AnswerQuestionId').on('change', function() {
   var questionID = $(this).val();
   // send ajax
   $.ajax({
      url: 'admin/answers/add/' + questionID,
      method: 'get',
      dataType: 'json',
      success: function(response) {
          // for example
          // response = [{'Answer': {'id': 1, 'answer': 'ans 1'} }, {'Answer': {'id': 2, 'answer' : 2}}....];
          // now loop over the response
          var html = '';
          $.each(response, function(index, val) {
              html + = '<div id="answer_'+ val.Answer.id +'">'+ val.Answer.answer +'</div>'
          });
          // append this html to target container
          $('#target_container').append(html);
      }
   });
});

在CakePHP方面尝试如下:

public function admin_add($id = null) {
    if( !empty($id) )
    {
        $this->Answer->recursive = -1;
        $answers = $this->Answer->find('all', 
                                        array(
                                         'fields' => array('Answer.answer', 'Answer.id'), // assume the answer contains in answer field in db
                                         'conditions' => array('Answer.question_id' => $id)
                                        )
                                      );
        echo json_encode( $answers );
    }
}