Answer the question
In order to leave comments, you need to log in
Yii2 how to open a modal window on a specific url?
Hello colleagues!!
Help solve the issue pliz!
I make a login form on the site in a modal window, and everything seems to be a class, everything works, I
click on the button, the window with the form opens.
But the question arose of how to make it so that if the user leads in the address bar, /login
did not transfer him to this address, but simply opened a window
, here are parts of the code, so that there would be fewer unnecessary questions
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
if ($model->login()) {
return $this->goBack();
} else{
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
}
return $this->renderAjax('ajax-login', [
'model' => $model
]);
}
$('#modal-btn').on('click', function () {
$('#modal').modal('show')
.find('#modal-content')
.load($(this).attr('data-target'));
});
<a id="modal-btn" data-target="<?php echo \yii\helpers\Url::to('/login')?>">Вход</a>
<?php
yii\bootstrap\Modal::begin([
'header' => 'Оформить заказ',
'id' => 'modal',
'size' => 'modal-md',
]);
?>
<div id='modal-content'>Загружаю...</div>
<?php yii\bootstrap\Modal::end(); ?>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="color:#999;margin:1em 0">
<?= Html::a(Module::t('module', 'LINK_PASSWORD_RESET'), ['password-reset-request']) ?>.
</div>
<div class="form-group">
<?= Html::submitButton(Module::t('module', 'USER_BUTTON_LOGIN'), ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
on the advice of Maxim Timofeev, I solved the issue like this
$(function () {
if (location.hash == '#login') {
var login_form = $('#modal-login-form');
$('#modal').modal('show')
.find('#modal-content')
.load(login_form.attr('data-target'));
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question