• PHP Help
  • How to pass form parameters to two actions using two submit buttons

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
  }
  public function actionExport() 
  {
  // When user clicks on Export button of the form then form parameters will be loaded and this action should execute.
  
  }
  }

    If I recall my HTML, the two buttons would need different names, and you'd look to see which one was present in the submitted form. But then there's whatever your framework is doing getting between that. Maybe using input buttons might be necessary.

    Weedpacket
    I tried adding different name attributes to both submit buttons and tried checking whether they are set in both the actions still it is not working as expected

    Controller

    <?php
    class Ascteacherreprt extends Controller
    {
    public function actionIndex()
    {
    if(isset($_GET['search1']))
    {
    // code to execute when user clicks on Search button of the search form
    }
    }
    public function actionExport()
    {
    if(isset($_GET['export']))
    {
    // code to execute when user clicks on Export button of the search form
    }
    
    }
    }

      Possibly, just my opinion, but using GET for form submission seems a tad likely to be wonky. Are you testing via a browser? Are you seeing the params being passed in the URLs?

      dalecosp
      Yes the parameters are coming in the URL. But the export action is not getting executed.

        How does your framework's controller infrastructure work to tie action methods to requests? Like, what do you do to declare that actionIndex should be called if the submit button is clicked (or actionExport if the "other" submit button is clicked)?

        Because it may be that actionIndex should take what is submitted either way, and then delegate to a Search or Export method based on the content of the request.

          As far as I recall, Yii bases the controller handling method on the URL; like http://localhost/form/example would call FormController::actionExample. Either way, you'll need to set your form's action to a route that Yii can parse; in that form, create two submit buttons with the same name attribute. The 'Search' button should have a name of search and the 'Export' button should have a name of export. Then in the controller's form submission method, run the appropriate method to the value of the submit button(s) index in the request array. Something along the lines of this:

          <form action="/form/parse" method="get">
          	<fieldset>
          		<input type="submit" name="action" value="search">
          	</fieldset>
          	<fieldset>
          		<input type="submit" name="action" value="export">
          	</fieldset>
          </form>
          
          <?php
          
          class FormController{
          	public function actionParse(){
          		if($_GET['action'] == 'search'){
          			$this->searchResults($_GET);
          		}elseif($_GET['action'] == 'export'){
          			$this->exportResults($_GET);
          		}else{
          			//throw a 404 or something...
          		}
          	}
          }
          

            The problem with using the submit button's value is that the string there is supposed to be UI text for the user; having it leak into your controller would mean extra work if someone down the line decides the button needs a friendlier label (or implements localisation for different users).

            Giving the buttons different names and checking to see which is included in the submission (non-submitting buttons are not included in form submission) would be more reliable.

            <form action="/form/parse" method="get">
            	<fieldset>
            		<input type="submit" name="searchaction" value="Recherche">
            	</fieldset>
            	<fieldset>
            		<input type="submit" name="exportaction" value="Exporter">
            	</fieldset>
            </form>
            class FormController{
            	public function actionParse(){
            		if(isset($_GET['searchaction'])){
            			$this->searchResults($_GET);
            		}elseif(isset($_GET['exportaction'])){
            			$this->exportResults($_GET);
            		}else{
            			//throw a 404 or something...
            		}
            	}
            }

            Is that permissible when using Yii? Is the name action significant?

            Weedpacket The problem with using the submit button's value is that the string there is supposed to be UI text for the user; having it leak into your controller would mean extra work if someone down the line decides the button needs a friendlier label (or implements localisation for different users).

            Valid point; I hadn't considered that. Maybe using a button element would be more appropriate in that case?

            As to action in Yii-land, yeah so far as I remember Yii's conventions dictate that a method be prepended with 'action' in order to fire automatically. So as in the example, the route /form/parse would by default fire the FormController::actionParse method. You can override this, but I believe that's the default. Full disclosure, it's been more than a couple years since I've had to deal heavily with Yii and even at the time it wasn't my favorite experience...

              Write a Reply...