开发者

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 c开发者_JAVA技巧html: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.


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);

}


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


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜