I have a quick search form
<?php yii \ widgets \ Pjax :: begin(['id'=>'new_note'])?>
      <?=Html :: beginForm(['/system/system/fastsearch'],'post',['data-pjax'=>'','enctype'=>'multipart/form-data','class'=>'form-inline'])?>
      <?=Html :: input('text','search',"",['class'=>"form-control form-control-sm ml-3 w-75"])?>
      <?=Html :: submitButton('<i class="glyphicon glyphicon-search"></i>&nbsp;Find',['class'=>'btn btn-default','id'=>'press_search'])?>
      <?=Html :: endForm()?>
      <?php Pjax :: end();?>

The view is displayed in actionIndex();The search request is passed to actionFastsearch();
The question is how to display the data in the desired div?
And why do I have to go to url in action?
  • No way better, it won't be in Yii3) – Blue66 Jul 18 '19 at 19:59

1 Answers 1

Good morning.
Pjax deals only with content that lies between begin() and end().
Therefore, you can simply add the desired div after the form.
Form code.
<?Php
   yii \ widgets \ Pjax :: begin(['id'=>'new_note','enablePushState'=>false])?>
      <?=Html :: beginForm(['/system/system/fastsearch'],'post',['data-pjax'=>'1','enctype'=>'multipart/form-data','class'=>'form-inline'])?>
      <?=Html :: input('text','search',"",['class'=>"form-control form-control-sm ml-3 w-75"])?>
      <?=Html :: submitButton('<i class="glyphicon glyphicon-search"></i>&nbsp;Find',['class'=>'btn btn-default','id'=>'press_search'])?>
      <?=Html :: endForm()?>
      <div id=" reload-pjax"><?=$search ?? null?></div>
  <?php Pjax :: end();
?>

The action in the controller can be like this.
public function actionFastsearch()
{
    if(Yii :: $app->request->isAjax) {
        $search=Yii :: $app->request->post('search');
        return $this->render('index',['search'=>$search]);
    }
}

Or, in my opinion, it’s better to submit the form and process the server response using regular js or jquery.

P.S.
Although you can use this option.The form must be filled in.
<?Php
Pjax :: widget([
   'id'=>'reload-pjax',
   'enablePushState'=>false,
   'enableReplaceState'=>false,
   'formSelector'=>'#options-form',
   'submitEvent'=>'submit',])
?>
<?=Html :: beginForm(['/site/fastsearch'],
                   'post',
                   [
                       'class'=>'form-inline',
                       'id'=>'options-form'
                    ])?>
<?=Html :: input('text','search',"",['class'=>"form-control form-control-sm ml-3 w-75"])?>
<?=Html :: submitButton('<i class="glyphicon glyphicon-search"></i>&nbsp;Find',['class'=>'btn btn-default','id'=>'press_search'])?>
<?=Html :: endForm()?>
<div id=" reload-pjax"></div>
  • Fabulous60.Everything works fine, except that the problem is the same - the script connected to index stops working for me.How to be?
    <?php
    $script=<<<JS
    $(document) .ready(function() {
            $('table tbody tr').on("click","tr", function() {
              alert(43);
            });
    });
    
    Js;
    $this->registerJs($script, yii \ web \ View :: POS_END);
    
    ?>

    Something you do not want to re-connect JS in the returned file, how to be?
    – Mythical43 Jul 17 '19 at 16:13
  • Mythical43, try connecting pjax again after completing pjax. – Fabulous60 Jul 17 '19 at 16:38