N
N
Nikolai Novosad2015-10-08 09:55:12
JavaScript
Nikolai Novosad, 2015-10-08 09:55:12

Ajax form submission in Yii2?

Hello.
Here is a dropdown list. When you select the desired item and click on the button, the data 'flies' to the controller, where I then work with them.
But how to make the data 'fly' to the controller without pressing the button?
In a simple script, I would write like this:

$("#mUnit").change(function() {
            var unit = $("#mUnit :selected").val();
            $.ajax({
                type: "POST",
                url: "url.php",
                data: {"unit": unit},
                cache: false,
                success: function(response) {
                    $(".money").html(response);
                }
            });
        });

And how to implement such functionality in yii2? If we take this script as a basis, then where to prescribe the script? In view? Where should url.php be? How to get data to the controller?
Or is Pjax suitable for this functionality?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Khomenko, 2015-10-08
@nik_neman

then where to prescribe the script?

1. In JS, which is connected to the AssetBundle, which is registered in the View
2. Through registerJs () directly in the View
Instead of url.php, you should have a controller action method, where you pass data, for example, to site/url
No, I don't think it can be used in this case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question