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.


        type : 'post',

        url : ajaxUrl+'controller/action',

        success : function(data){


            var result = $.parseJSON(data);



        error : function (){




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.