K
K
kolyafat2016-09-26 23:28:43
Yii
kolyafat, 2016-09-26 23:28:43

A hackneyed question - ajax+CHtml::dropDownList, doesn't work please?

Good health to all, please help me figure out how to send a POST select via ajax. Here is the code:

echo CHtml::dropDownList('listname', 'bla',
              $model,
        array('empty' => 'Марка авто',
              'style'=>'width:155px',
              'class'=>'btn btn-default dropdown-toggle',
              'ajax' => array(
                        'type'   => 'POST',
                        'url'    => CController::createUrl('site/getMod'),
                        //'data'=> array(
                         //   'id'=>'js:this.value',
                         //   'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
                        'update' => '#sub_cat_id',),
            
            ));

It's in the view. The controller is not important, you just need the site/getMod request to return something. How to organize this, please help, a task in Yii, I used to do very little on it.
public function actionGetMod()
  {         
            $ff = array();
                      return $ff;            
  }

This is where the request flies, when you try to insert input values actionGetMod($id)
, a bad request 400 appears, where I'm stupid, help.
<div  id="sub_cat_id">
     <?php    print_r($ff);      ?>
    </div>

As I understand it, the result should be updated here, but nothing happens, there are no errors either OK200. Thanks in advance to those who sympathize.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Konyukhov, 2016-09-27
@heahoh

Are you passing id in GET?

K
kolyafat, 2016-09-27
@kolyafat

In general, for a long time I will smoke all kinds of scriptures, there are thoughts in the direction of CHtml::$liveEvents = FALSE; but it’s of little use, I don’t have ice with JavaScript either, it turns out the following heresy, which I didn’t expect from myself and from my Yii:
The first select is in my view and there are no problems with it

CHtml::$liveEvents = FALSE;
echo CHtml::dropDownList('listname', 'bla',
              $model,
        array('empty' => 'Марка авто',
              'style'=>'width:auto',
              'class'=>'btn btn-default dropdown-toggle',
              'ajax' => array(
                        'type'   => 'POST',
                        'url'    => CController::createUrl('site/getMod'),
                        'data'=> array(
                            'id'=>'js:this.value',
                            'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
                        'update' => '#sub_cat_id',                  
                  ),
            
            ));

When selected, it sends ajax, which in turn returns the second select
public function actionGetMod()
  {
  CHtml::$liveEvents = FALSE;
       if(Yii::app()->request->isAjaxRequest){
        if(isset($_POST['id'])){
            $f= Mod::getModelData($_POST['id']);
            echo CHtml::dropDownList('modelky', 'mode',
              $f,
       array( 'empty' => 'Модель авто',
             'style'=>'width:155px',
              'class'=>'btn btn-default dropdown-toggle',
              'ajax' => array(
                        'type'   => 'POST',
                        'url'    => CController::createUrl('site/getVar'),
                        'data'=> array(
                        'id_v'=>'js:this.value',
                        'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
                        'update' => '#var',
                   'succes' => 'function(){$("#var").change();}',
                  ),
           
           ));  
            
        }           
  }
        }

It returns, it is displayed, only this ajax select is no longer sent, but I really want to.
Please, knowledgeable people, 100% have come across Yii superwidgets, help, in JS, because I have a deuce. In the page code after the first ajax here
/*<![CDATA[*/
jQuery(function($) {
jQuery('#listname').on('change', function(){jQuery.ajax({'type':'POST','url':'/site/getMod','data':{'id':this.value,'YII_CSRF_TOKEN':'d02ef985c3400d717ee866ede2af6610af42b446'},'cache':false,'success':function(html){jQuery("#sub_cat_id").html(html)}});return false;});
});
/*]]>*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question