Question

i have a dropdownlist

modelA

 <?php echo $form->labelEx($model,'type'); ?>
    <?php echo $form->dropDownList($model,'type',array('empty'=>'choose refertype','1'=>'typeA','2'=>'typeB','3'=>'typeC')); ?>
    <?php echo $form->error($model,'type'); ?>

following is my ajax code what it must do is when i select typeA or any other type it must fetch data from db (depending on the type of modelA data from db of modelB must be fetched )and display in #showdata div which is not working when

$('#modelname_type').change(function(){

    if($('#modelname_type').val() == '1'){
        $('#showdata').show();
        $.ajax(
            'url':'Yii::app()->createUrl('controllerB/actionB')',
            'type':'get',
            'data':array('id'=>$_GET['id']),
            'success': function(res){       
                $("#showdata").html(res);
            }       
        );          
        $('#codea').hide();     
    }   
});

works only for

$('#modelname_type').change(function(){

    if($('#modelname_type').val() == '1'){
        $('#showdata').show();          
        $('#codea').hide();     
    }
});

Please let me know where am i going wrong please am really stuck from past week am not that strong with ajax

Was it helpful?

Solution 2

here

 if($('#modelname_type').val() == '1'){

change to

  var mtype =$('#modelname_type').val();
    if(mtype == '1'){ 

IN AJAX THE

url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/controllerB/actionB/id/"+str';

Will work like a charm

OTHER TIPS

For Dependent Dropdown list You can Check This Link contains based on country , specific states populated on other dropdown list..

https://stackoverflow.com/a/22169219/3012139

Proper use of ajax call in jQuery is:

$.ajax(
    'url': 'your_script.php',
    'type': 'get',
    'data': array,
    'success': function (res) {
         $("#showdata").html(res);
    }
});

where array is a javascript array.

Your code doesn't work, because url and data are not proper. Javascript won't execute PHP code.

Here are tutorials you may find useful:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top