PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] how to change values in a dropdown list using ajax in Yii

 January 23, 2022     ajax, drop-down-menu, jquery, post, yii     No comments   

Issue

in view page i have 2 dropdown

<?php
$list = CHtml::listData($category, 
                'cid', 'cname');    
echo $form->dropDownList($model,'cid',$list,
              array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>
<?php  
$subcategory = array();
echo $form->dropDownList($model,'sid',$subcategory,
              array('empty' => '(Select a subcategory)')); 
?> 

Using value from category i have to change subcategory values

Here is my ajax function

<script type="text/javascript">
    $(function (){ 
        $("#Product_cid").change(function (){             
            var cid = $('#Product_cid').val();
            var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
            $.ajax({  
            type: "POST",      
            url: path, //url to be called
            data: { cid: cid } //data to be send 
            }).done(function( msg ) {
                 alert(msg)
                 $("#Product_sid").val("msg");
                 $("#Product_sid").selectmenu('refresh'); 
.
             });

        });
    });
</script>

And here is my controller

public function actionMysubcategory()
{
        $cid = Yii::app()->request->getPost('cid'); 
        $subcategory= Subcategory::model()->findAll(
                array('order'=>'sid',
               'condition'=>'cid=:cid', 
               'params'=>array(':cid'=>$cid)));
            $list = CHtml::listData($subcategory, 
            'sid', 'sname');    

        print_r($list);
} 

enter image description here

I gets list from controller. How can make into a dropdown???


Solution

In Your controller you can

public function actionMysubcategory(){
    $model = new YourModel();
    $cid = Yii::app()->request->getPost('cid'); 
    $subcategory= Subcategory::model()->findAll(
            array('order'=>'sid',
           'condition'=>'cid=:cid', 
           'params'=>array(':cid'=>$cid)));
        $list = CHtml::listData($subcategory, 
        'sid', 'sname');
       echo CHtml::activeDropDownList($model,'sid',$list);
    } 

In your view you can do something like this

<?php
       $list = CHtml::listData($category, 
            'cid', 'cname');    
       echo $form->dropDownList($model,'cid',$list,
             array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>

 <div id="sub-category-wrapper">
 <?php  
      $subcategory = array();
      echo $form->dropDownList($model,'sid',$subcategory,
          array('empty' => '(Select a subcategory)')); 
  ?>
</div>

Then in your ajax

<script type="text/javascript">
$(function (){ 
    $("#Product_cid").change(function (){             
        var cid = $('#Product_cid').val();
        var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
        $.ajax({  
        type: "POST",      
        url: path, //url to be called
        data: { cid: cid }, //data to be send
        success: function( response ) {
             $('#sub-category-wrapper').html(response);
         }
        })

     });
 });



Answered By - Rasikh Mashhadi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing