php - How to populate the select list by jquery ajax in cakephp -


i using cakephp, , want selected degree id , want display subjects belongs selected degree in drop down list. degree select list below

<div class="form-group">   <?php echo $this->form->input('primaryregister.degree', array(      'options'=>$degrees,      'empty'=>'-- select 1 --',      'label' => false,       'class' => 'form-control',      'id' => 'degree',      'required'=>'required')    ); ?> </div> 

and jquery below

$('#degree').change(function(){     var degree_id=$(this).val();     var base_url='<?php echo $this->webroot; ?>';     $.ajax({         type:'post',         data:'degree_id='+degree_id,         datatype:'json',         url:base_url+'/pages/bla',         async:false,         success: function(data){             $(data).each(function() {              $('.test').append($("<option>").attr('value',this.val).text(this.text));             });         }     }) }); 

the result want display below

<div class="form-group">                             <label>main/core<sup class="madadatory">*</sup></label>                             <?php                             echo $this->form->input('primaryregister.mainsubject1',array(      'label'=>false,      'options'=>'',      'empty'=>'-- select 1 --',      'class'=>'form-control test',      'id'=>'mainsubject1',      'required'=>'required'              )           );        ?> </div> 

note: getting array ajax result, below

<pre>array ( [1] => zoology  [2] => botany  [3] => plant science  [4] => home science  [5] => forestry  [6] => microbiology  [7] => chemistry  [8] => polymer chemistry  [9] => biochemistry  [10] => biotechnology  [11] => physics  [12] => psychology  [13] => geology  [14] => mathematics  [15] => computer science  [16] => electronics  [17] => geography  [18] => statistics [19] => bioinformatics [20] => electronics & communication  [21] => acqua culture , fishery microbiology? [22] => applied statistics [23] => applied physics [24] => chemistry [25] => botany [26] => costume , fashion technology [27] => counselling psychology [28] => genetics [29] => environmental science , water management [30] => family , community science [31] => food technology ) 

subject selecting field generating jquery only, please me

you can generate html string in view after fetching them in action , print string on view of action.

public function your_function() {     // fetch data     // set array view } 

on view

foreach($your_array $key => $value) {    echo "<option value='$key'>$value</option>; } 

the ajax html response. , set html -

$('.your_drop_down_menu').html(response); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -