Yii2 Ajax .post to controller from dropdownList of

2019-07-20 23:02发布

I have a DropDown list. I've written the code but it's not working. Please help me to fix it:

echo $form->field($model, 'Adrop')->dropDownList(
    [
        '' => 'Please Choose',
        '1' => 'item 1',
        '2' => 'item 2'
    ],
    [
        'onchange' => '$.post(Yii::$app->urlManager->createUrl . "users/A_action"), function(data) {
            $("#test_div").html(data)
        }'
    ]
);

Also I want to send selected data, and don't know where to write it.

In the Controller I have this action

 public function actionA_action() {
     $data = "TTT";
     return $data;
 }

Now when I select something in the DropDown list, nothing happens in my test_div :(

UPDATE Thanks to Mihai P. now I'm using this code

<?php
      echo   $form->field($model, 'Adrop')->dropDownList(
          [''=>'Please Choose','1'=>'item 1','2'=>'item 2'],
          [
          'onchange'=>'$.post( "'.Yii::$app->urlManager->createUrl(["users/A_action"]).'",function(data){
                $("#test_div").html( data )
                }']);
        ?>

The HTML is formed in the following way

<select id="A-adrop" class="form-control" name="A[Adrop]" onchange="$.post( &quot;/users/A_action&quot;,function(){
                $(&quot;#test_div&quot;).html( data )
                }">
<option value="">Please Choose</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
</select>

But when I choose something in debug this string is highlighted

 <option value="2">item 2</option>

and there is one error saying

Uncaught SyntaxError: Unexpected token }

Final UPDATE

I've added one closing bracket on the last string of this code there are two of them closing now as you can see, and that was the problem. Semicolumn also will be a plus, but I've tested code works without it OK. problem was in closing bracket.

 'onchange'=>'$.post( "'.Yii::$app->urlManager->createUrl(["users/A_action"]).'",function(data){
                    $("#test_div").html( data );
                    })']);

3条回答
我命由我不由天
2楼-- · 2019-07-20 23:07

Just go through these codes, you may understand the working

    $(document).ready(function () {
        $(document.body).on('change', 'yourid', function () {   
            var val = $('yourid': selected').val(); 
            if(val == 'I' ) {
                something
            } else if(val == 'B' ){
                something
            }
        });
    });
查看更多
可以哭但决不认输i
3楼-- · 2019-07-20 23:33

well I am sure you have a javascript error. You should actually have a lot of them too.

You are doing this

'onchange' => '$.post(Yii::$app->urlManager->createUrl . "users/A_action"), function(data) {
            $("#test_div").html(data)
        }'

You are not actually calling Yii::$app->urlManager->createUrl you are just using it as a string.

You probably need something like

...
['onchange' => '$.post("'.Yii::$app->urlManager->createUrl(["users/A_action"]).'", function( data ) {
      $("#test_div").html( data );
     })']);
查看更多
太酷不给撩
4楼-- · 2019-07-20 23:33

Simple add a JS block, it is much more clean:

<?php $this->registerJs("
    jQuery(function($){
        $('select[name=Adrop]').on('change', function(){
             $.post(Yii::$app->urlManager->createUrl . 'users/A_action'), 
             function(data) {
                   $('#test_div').html(data)
             }
        });
    });");?>
查看更多
登录 后发表回答