简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM