Update data on modal dialog.

Help me please.

I try to config when user click update button.

  1. data on form update to database.

  2. close modal dialog.

  3. refresh data in CgridView.

Thank you very much.

drive.google.com/

file/d/0B_1HWtKKDAK_emZhUkI3TV9xUE0/view?usp=sharing

Post the code you’ve written so far.

Basic workflow:

-Modal code in your page, hidden by jquery until button clicked.

-When clicked, jquery will display the modal which loads a url containing your form (i.e. /action/create)

-When submit is clicked, ajax request to server. Handle post data, return response. Jquery code to hide modal again.

-In ajax ‘success’ statement, handle server response. If fail, show error message. If success, refresh grid.

I am using pjax refreshing for my gridview which is pretty handy.

I learn and do in web (http://www.yiiframework.com/wiki/690/render-a-form-in-a-modal-popup-using-ajax/).

But it is redirec to view.

Thank you.

----------PhotoController extends Controller----------




    public function actionCreate()

    {

        $model = new Photo();


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

            return $this->actionIndex();

            //return $this->redirect(['view', 'id' => $model->pID]);

        } else {

            return $this->renderAjax('create', [

                'model' => $model,

            ]);

        }

    }



----------index.php----------




    <p>

        <?= Html::a(Yii::t('app', 'Create {modelClass}', [

    'modelClass' => 'Photo',

]), ['create'], ['class' => 'btn btn-success modalLink']) ?>

    </p>




<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

         ******

         ******

            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]) ?>


 




<?php

Modal::begin(['id' => 'modal']);


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


Modal::end();

?>



---------- _form.php ----------




    <?php $form = ActiveForm::begin([

           'enableAjaxValidation'=> true,


    ]); ?>


    <?= $form->field($model, 'eID')->textInput(['maxlength' => 4]) ?>

    ************

    ************

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


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



---------- jquery ------------




// listen click, open modal and .load content

$('.modalLink').click(function (){

    $('#modal').modal('show')

        .find('#modalContent')

        .load($(this).attr('href'));  //value

      //  $('#modalContent').toggle();

        return false;

});


 

// serialize form, render response and close modal

function submitForm($form) {

    $.post(

        $form.attr("action"), // serialize Yii2 form

        $form.serialize()

    )

        .done(function(result) {

            $form.parent().html(result.message);

            $('#modal').modal('hide');

        })

        .fail(function() {

            console.log("server error");

            $form.replaceWith('<button class="newType">Fail</button>').fadeOut()

        });

    return false;

}