Jump to content

How to pass form data/parameters to two actions using two submit buttons in Yii2


PrashantS

Recommended Posts

I am using Yii 2 framework and I have a search form with input fields and 2 submit buttons Search and Export. When the user clicks on Search button then index action should execute and when user clicks on Export button then export action should execute. Now even if user clicks on Export button export action is not executed.

Below is the search form

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Asccenter;
use app\models\Ascuser;
?>

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

 <?= $form->field($model, 'ASCId')->dropDownList()?>   <?php //Input field 1?>
 <?= $form->field($model, 'UserId')?>  <?php // Input field 2?>
 <?= $form->field($model, 'Year')?>  <?php // Input field 3?>

<div class="form-group">
 <?php // Search button. When user clicks on Search button then index action should execute ?>
<?= Html::submitButton('Search' ,['class' => 'btn btn-primary','id'=>'searchbutton','name'=>'search1','value'=>'search2']) ?>
 
  <?php //Reset button to clear the form inputs?>
<?= Html::a('Reset',['index'], ['class' => 'btn btn-default']) ?>
  
  <?php // Export button. When user clicks on Export button, then export action should execute?>
<?= Html::submitButton('Export', ['class' => 'btn btn-default','id'=>'exportbutton','name'>'search1','value'=>'export1']) ?>

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

Below is the Controller

<?php

namespace app\controllers;
use Yii;
use app\models\Ascteacherreport;
use app\models\AscteacherreportSearch;
use yii\helpers\ArrayHelper;

class AscteacherreportController extends Controller
{
public function actionIndex()
    {
  // When user clicks on Search button then this action should execute
$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,
        ]);


    }
  
  public function actionExport() 
  {
  // When user clicks on Export button of the form then form parameters will be loaded and this action should execute.
  
  }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.