Yii2 Ajax .post to controller from dropdownList of view and some action upon receiving data

你离开我真会死。 提交于 2019-12-24 04:47:09

问题


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 );
                    })']);

回答1:


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 );
     })']);



回答2:


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)
             }
        });
    });");?>



回答3:


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
            }
        });
    });


来源:https://stackoverflow.com/questions/27757648/yii2-ajax-post-to-controller-from-dropdownlist-of-view-and-some-action-upon-rec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!