3

I have a Modal popup, after the user submit the data, I want to show the results on the same popup. How do I reload that modal view? Thanks.

popup view

This is the popup view:

<div class="site-popup">
    <h1><?= Html::encode($this->title) ?></h1>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'popup-form']); ?>
                <?= $form->field($model, 'pattern')->textArea(['rows' => 1]) ?>

                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'popup-button']) ?>
                </div>

            <?php ActiveForm::end(); ?>
        </div>
</div>

 <?php
    echo "<pre>";
    print_r($model->data); 
 ?>

This is the view that calls to the above modal popup view:

<p>
    <?= Html::button('Search', ['value' =>Url::to('index.php?r=site/mypopup'),'class' =>'btn btn-success', 'id'=>'modalButton']) ?>
 </p>
        <?php
            Modal::begin([
            'header'=> '<h4>Search</h4>',  
            'id' => 'modal',
            'size' => 'modal-lg',
            ]);

            echo "<div id='modalContent'></div>";

            Modal::end();
       ?>

The js file:

$(function(){
    //get the click of the search button
    $('#modalButton').click(function(){
        $('#modal').modal('show')
            .find('#modalContent')
            .load($(this).attr('value'));
        }); 
});

Controller:

public function actionPopup()
{

 $model = new PopupForm();

   if ($model->load(Yii::$app->request->post()) && $model->validate()) 
   {
      $model->insertPopup();
         return $this->renderAjax('mypopup', ['model' => $model]);

            } else {
                return $this->renderAjax('mypopup', [
                    'model' => $model,
                ]);
            } 
}
Jan Beeck
  • 303
  • 8
  • 24

2 Answers2

2

have you tried to wrap block with Pjax?
Yii2 will handle form submit, and display results from controller in modal popup.

<div class="site-popup">
    <h1><?= Html::encode($this->title) ?></h1>

    <div class="row">
        <div class="col-lg-5">
            <?php Pjax::begin(['enablePushState' => false]); ?>
                <?php $form = ActiveForm::begin(['id' => 'popup-form']); ?>
                <?= $form->field($model, 'pattern')->textArea(['rows' => 1]) ?>

                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'popup-button']) ?>
                </div>

                <?php ActiveForm::end(); ?>
            <?php Pjax::end(); ?>
        </div>
    </div>
</div>
Azamat
  • 98
  • 1
  • 1
  • 5
  • I have tried the above (wrap the whole ActiveForm with Pjax), but the popup view is not reloading after the data is submitted. What else need to be done? Thanks. – Jan Beeck Jun 28 '17 at 04:45
0

You are using the click event on the button. Instead of click event in js you would need to call submit event like this.

$(function(){

    //get the click of the search button
    $('.site-popup form').submit(function(){
        $('#modal').modal('show')
            .find('#modalContent')
            .load($(this).attr('value'));
        }); 
       return false
});
Pang
  • 9,564
  • 146
  • 81
  • 122
  • Thanks for the answer @Ramesh Mahajan, but the popup view is not appearing with the above code :( What could be wrong on the js file? Thanks. – Jan Beeck Jun 28 '17 at 17:04