简体   繁体   中英

Yii2 how to use pjax to submit a form without closing bootstrap modal popup

I'm using bootstrap modal for login page. If i submit the form in popup will be closed and shown response. But instead of closing popup, would like to display response in same popup window. Could anyone help me without closing the popup to show the response data.

Sample View Page:

<?php Pjax::begin(); ?>
<?= Html::beginForm(['site/login'], 'post'); ?>
<?= Html::input('text', 'email', ['class' => 'form-control']) ?>
<?= Html::input('text', 'password', ['class' => 'form-control']) ?>
<?= Html::submitButton('Login', ['class' => 'btn btn-lg btn-primary', 'name' => 'login']) ?>
<?= Html::endForm() ?>

In Controller

if ($model->load(\Yii::$app->request->post())) {

     $authenticateUser = $model->login();
     if($authenticateUser == "Locked"){
        Yii::$app->session->setFlash('invalid', 'Your account has been locked. Please contact customer service for assistance');
                return $this->redirect('index');
     }else{
        Yii::$app->session->setFlash('success', 'Successfully logged in');
                 return $this->redirect('index');
     }
}

In Controller, Instead of redirect ,i've tried with renderAjax also. But it is not working as expected.

You need to enforce form submission via ajax by adding your own javascript handler beforeSubmit . See this link .

Try below one

use yii\widgets\Pjax;
use yii\bootstrap\ActiveForm;

<?php Pjax::begin(); ?>
    <?php 
      $form = ActiveForm::begin([
        'id' => 'form-id',                   
        'options' => ['class' => 'form-horizontal','data-pjax' => true],
      ]);  
    ?>
        // Your fields
    <?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM