How to render form using onchage function in Ajax?

I would like to render a different form depending on the choice of options in the drop-down list.

Could you give me a sample code?

Using Ajax pass your data to controller.


$.ajax({

        type : 'post',

        url : ajaxUrl+'controller/action',

        success : function(data){

            //alert("Success");

            var result = $.parseJSON(data);

            $("#id_for_render_form").html(result.output);

        },

        error : function (){

            alert("Error");

        }

});

In controller, render a form based on your selection and then return a form in JSON format like


$output = $this->render('Your form');

return JSON::encode(['output' => $output]);

Then append the form in the id which is represented in the view page.