Abhinav
Abhinav

Reputation: 97

Yii framework: passing variable through Chtml:button

I am trying to pass a variable from a view (of mobile model) to a different controller (of inventory model), using the chtml:button with this code

echo CHtml::button(
    'Sell It', 
    array('submit' => array('inventory/create', array('id'=>$data->id)))
);

Now how do I access the $id variable in the Inventory controller, so that I can prepopulate the create view with details corresponding to the passed 'id' variable of the mobile model.

Upvotes: 0

Views: 5961

Answers (4)

veelen
veelen

Reputation: 1914

in your controller you can get the variable by giving an argument to your controller method like this:

public function actionCreate($id){
    $id = isset($id)?$id:NULL; // Or whatever, you can access it like this.
}

You don't have to use $_GET, and yii has already done some security checks on the value.

Upvotes: 0

vijayakumar.k.r
vijayakumar.k.r

Reputation: 21

In your inventory/create controller action do checking before getting the id like this :-

if (isset($_REQUEST['id'])) {

    $id = $_REQUEST['id'];

    $this->render('create',array('model'=>$inventory, 'id'=>$id));

}
else{

    $this->render('create',array('model'=>$inventory);

}

Upvotes: 1

ldg
ldg

Reputation: 9402

In your inventory/create controller action do a test for $_GET['id'] something like:

$id = (@$_GET['id']) ? : DEFAULT_VALUE;
$this->render('create',array('model'=>$inventory, 'id'=>$id));

and then you pass the data through to the view by passing an array of variables you want to make available.

(you would want to filter the input better, this is just a sample -- using filter_input or some other method and define a default value and/or some test for it being null/invalid)

Upvotes: 0

Vijay
Vijay

Reputation: 5433

If you are trying to come up an update/edit form with the values prefilled based on the Id passed then you should have to go through the CRUD options available within YII.. This is much better way to handle record updation and its easy too . See this topic for furhter info..

http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app

Upvotes: 0

Related Questions