更改< select>选项时,如何在cakephp中检索数据.领域? [英] How to retrive data in cakephp when change the options of a <select> fields?

查看:75
本文介绍了更改< select>选项时,如何在cakephp中检索数据.领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Cakephp的新手.我有两个名为问题" 答案" 的控制器.在答案视图中,我有一个admin_add.ctp文件,其中包括一个显示问题列表的选择框.但是问题是当某人选择问题时,尽管我的admin_add.ctp包含一个输入字段,但是它将根据所选的question_id在不同的输入字段中显示所有答案.因此,当有人进行更改时,必须将这些字段动态添加到admin_add ctp中.我的答案表包含字段"question_id"作为foreign_key.这是我的admin_add.ctp文件..

I am really new in cakephp. I have two controllers named 'questions' and 'answers'. In answers view i have a admin_add.ctp file which includes a select box that show the list of questions. But the problem is when someone select the question then it will show all the answers in different input fields depending on the selected question_id though my admin_add.ctp contains a single input fields. So the fields have to be added when dynamically to the admin_add ctp when someone make a change. My answers table contains a field 'question_id' as foreign_key. Here is my admin_add.ctp file..

<?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');?>  

有人可以帮助我吗?预先感谢.

Can anyone help me? Thanks in advance.

推荐答案

尝试如下所示:

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

Write change() method for that select box with AJAX request:

$('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方面,尝试如下操作:

In CakePHP side try like following:

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

这篇关于更改&lt; select&gt;选项时,如何在cakephp中检索数据.领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆