PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] How to pass variable from actionCreate to _form.php?

 February 16, 2022     yii, yii2     No comments   

Issue

I have a create action of controller where I passed $ingredients array into the view

public function actionCreate()
{
    return $this->render('create', 
        ['model' => $model, 'ingredients' => Ingredient::find()->all()]
    );
}

In create.php I can acess $ingredients variable. But if I try to access $ingredients in the _form.php

<?= $form->field($model, 'ingredient_id')->checkbox(
    ArrayHelper::map($ingredients, 'id', 'name')
) ?> 

I get error

Undefined variable: ingredients

Solution

This can be done by two ways
//first render the view file like this

    $params = [];
    $params['eqpType'] = $eqpType;
    $params['discountModel'] = $discountModel;
    $params['tabActive'] = $tabActive;
    $params['discountLabel'] = $discountLabel;

    echo $this->render('_form', $params);

//or you can include the code file directly ..in this case do not need to pass all perameters

    <?php include_once "_form.php";?>


Answered By - Prahlad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing