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.
Try like below:
Write
change()
method for thatselect
box with AJAX request:In CakePHP side try like following: