How to pass _search form parameters to two actions in Yii2

Hi, I am using Yii2. I have a controller called Ascteacherreport and search model for the same. I am using CRUD to create, update, view and delete the model. I have also used the search (_search form) on the index page. Now there are three buttons on search form, Search, Reset and Export. I want that when the user clicks on search button of the _search form then the search filter should work. When the user clicks on Export button then the search form parameters should be passed to different action.

How to pass _search form parameters to two actions using 2 buttons.

_search.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
?>
<div class="ascteacherreport-search">
<?php $form = ActiveForm::begin([
        'action' => ['index','ascteacherreport/export'], // index action for Search and export action to export the data
        'method' => 'get',
    ]); ?>

<?= $form->field($model, 'ASCId') ?>
 <?= $form->field($model, 'UserId')?>
 <?= $form->field($model, 'Year')?>

 <div class="form-group">
    <?= Html::submitButton('Search' ,['class' => 'btn btn-primary']) ?> // Search filter
    <?= Html::a('Reset',['index'], ['class' => 'btn btn-default']) ?> // To clear the form inputs
	<?= Html::submitButton('Export', ['class' => 'btn btn-default']) ?> // Pass the form data to export action
    </div>

You don’t need two action for export. Use export flag in index action itself to identify that request is for export. Your answer to add parameters to url is below

I have two actions in the controller : index and export

//When user clicks on Search button then index action should execute
 public function actionIndex()
    {
      $flag=1;
      $searchModel = new AscteacherreportSearch();
      if (!Yii::$app->user->can('indexAll')) {
            $searchModel->UserId = Yii::$app->user->identity->getonlyid();
		}

		$params = Yii::$app->request->getQueryParams() != null ? Yii::$app->request->getQueryParams() : [];
        $params[0] = Yii::$app->controller->getRoute();
        $params['new_param'] = 'ascteacherreport/export';  

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
		$query= Ascteacherreport::find();

        $count=$query->count();


        $pagination= new Pagination(['defaultPageSize' => 5,
                                    'totalCount' => $count]);

		$training = $query->offset($pagination->offset)->limit($pagination->limit)->all();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
			'pagination' => $pagination,
        ]);


    }
public function actionExport() //action to export the data based on the search form parameters. When user clicks on Export button then this action should execute
{


}

How do the code know which button user has clicked, Search button or Export button. If user clicks Search button then only search filter should work and if the user clicks Export button then export action should execute

You can use jQuery to change the action of the form.

I changed the _search.php as below, but not working as expected

  <?php $form = ActiveForm::begin([
        'method' => 'get',
		'id'=>'form1'
    ]); ?>

<?= $form->field($model, 'ASCId') ?>
 <?= $form->field($model, 'UserId')?>
 <?= $form->field($model, 'Year')?>

 <div class="form-group">
        <?= Html::submitButton('Search' ,['class' => 'btn btn-primary','id'=>'searchbutton']) ?>
        <?= Html::a('Reset',['index'], ['class' => 'btn btn-default']) ?>
		<?= Html::submitButton('Export', ['class' => 'btn btn-default','id'=>'exportbutton']) ?>
    </div>
<?php ActiveForm::end(); ?>

<script>
$('#searchbutton').click(function(){
$('#form1').attr('action','index.php?r=ascteacherreport/index');

});


$('#exportbutton').click(function(){
	$('#form1').attr('action','index.php?r=ascteacherreport/export-report');
});

</script>

You have to submit the form.

    $('#form1').attr('action', ... );
    $('#form1').submit();

Still the same, when user clicks on Export button then still the action export-report is not getting executed.

You are trying to create two identical actions except the export part, just pass the flag along with parameters for export in action later identify that flag in same action.

Where do I need to make changes in index action?

public function actionIndex()
    {
		$searchModel = new AscteacherreportSearch();
if (!Yii::$app->user->can('indexAll')) {
            $searchModel->UserId = Yii::$app->user->identity->getonlyid();
		}



        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
		$query= Ascteacherreport::find();

        $count=$query->count();


        $pagination= new Pagination(['defaultPageSize' => 5,
                                    'totalCount' => $count]);

		$training = $query->offset($pagination->offset)->limit($pagination->limit)->all();

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
			'pagination' => $pagination,
        ]);


    }

How should the form come to know that a user has clicked Search button and then execute index action and when user clicks on Export then execute export action.