K
K
Kirill Kaliev2020-04-21 20:30:44
Yii
Kirill Kaliev, 2020-04-21 20:30:44

How to display view on ajax request so that the form works on it?

I want to make the registration form appear on the site with Ajax when a button is clicked.
It turns out I need to display a view with a form on top of the index
Here is the js code when the register button is pressed

let regButton = document.querySelector('#reg_button');
regButton.onclick = function (e) {
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "site/reg",
    success: function (response) {
      $('#response').html(response)
    },
    error: function (response) {
      console.log(response)
    }
  });
};

Here is the code of the action that handles the request
public function actionReg()
  {
    if (Yii::$app->request->isAjax) {
      Yii::$app->response->format = Response::FORMAT_JSON;
      $model = new RegForm();
      return $this->renderAjax('reg', compact('model'));
    }
  }


As a result, it turns out that the answer is displayed and the form appears,
BUT it is without styles and scripts processing validation
5e9f2d6205b2a712245195.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question