V
V
Vladimir2017-09-01 15:41:35
Yii
Vladimir, 2017-09-01 15:41:35

How to submit a pjax yii2 form?

Good day! There is this form:

<?php Pjax::begin( [ 'enablePushState' => false, 'timeout' => 5000, 'id' => 'pjax_form' ] ); ?>
<?php $form = ActiveForm::begin( [
  'options'     => [ 'data-pjax' => true, 'enctype' => 'multipart/form-data', 'class' => 'contact-form' ],
  'id'          => 'upload',
] ); ?>

The fields in it are not ActiveForm, but ordinary:
<?= Html::activeInput( 'checkbox', $model, 'app_design', [ 'id' => 'app-design' ] ) ?>

And there are several of them, there is an activeTextarea and is responsible for sending:
<?= Html::submitInput( 'Submit', [ 'class' => 'submit' ] ) ?>

Here is the controller:
public function actionContacts() {

  $model = new Mails();
  if ( $model->load( Yii::$app->request->post() ) && $model->validate() ) {
    $model->save();
    if ( ! Yii::$app->request->isPjax ) {
      Yii::$app->session->setFlash( 'form_sended', '<h2 class="uppercase">Thank You!</h2><p>We will get back to you as soon as possible</p>' );

      return $this->redirect( [ 'contacts#thank-you' ] );
    }
  }

  return $this->render( 'contacts', compact( 'model' ) );
}

UPD: here's what else I found in the source of the page:
jQuery('#upload').yiiActiveForm([], []);
jQuery(document).pjax("#pjax_form a", {"push":false,"replace":false,"timeout":5000,"scrollTo":false,"container":"#pjax_form"});
jQuery(document).on("submit", "#pjax_form form[data-pjax]", function (event) {jQuery.pjax.submit(event, {"push":false,"replace":false,"timeout":5000,"scrollTo":false,"container":"#pjax_form"});});

Maybe something is wrong here?
UPD: I saw this in the console:
071d2b693e864a7ca69499c3b090ffa9.png
I have jQuery version 3.2.1 if anything.
After sending, it redirects to contacts#thank-you and the required field appears. But the page is reloading. Tell me what could be the problem? Thanks in advance for the replies!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2017-09-01
@MasterGerold

Pjax does not support jQuery 3.x
https://github.com/defunkt/jquery-pjax/issues/634
The link above in the comments recommends trying to use jquery migrate: https://code.jquery.com/jquery-migrate-3.0 .0.js
UPDATE : No, it works https://github.com/defunkt/jquery-pjax/issues/634#...
Try updating pjax to the latest version

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question