0

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.

4 Answers 4

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

no, this is not what I was trying to do. Anyways now using $_GET
0

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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.