Using Chtml::link with Post parameters

hi there,

Can somebody tell me how to pass parameters with the Post method through a link??

You create a form and put there some hidden fields. Then set the names/values of the hidden fields to represent the parameters you want to pass.

You also need to assign an id to the link and add a click event.

When the user clicks, you submit the form.

This also allows you to change the parameters at runtime.

Create the link like that:


Chtml::link(

   'label', 

    '', 

    array(

         'submit'=>array('url to submit to'), 

         'params'=>array('parameters to be sent via post')

    )

);

This method will do the same stuff suggested by genn (creating hidden fields with js and then submitting them)




CHtml::link(

'label',

'',

array(

'submit'=>array('controller/action'), 

'params'=>array('param1' => $data['id1'], 'param2' => $data['id2'])

)

);



I am trying this but nothing happens. And does the second parameter in the Chtml::link has to be empty single quote??

For general reference

I believe, that the second parameter is the url that is displayed by the browser in the statusbar (at least, in Firefox). The submit url in the htmlOptions array is the url where the link will actually take you to. For a good user experience, you should set these to the same value, e.g.


$url = array('controller/action');

CHtml::link(

	'label',

	$url,

	array(

		'submit' => $url,

		'params' => array('param1' => $data['id1'], 'param2' => $data['id2']),

	)

);

Can someone please show some sample code of what you would have in your controller to access/read these parameters.

Would it look like:




    public function actionMyAction() {

        $param1 = Yii::app()->request->getPost('param1');

        $param2 = Yii::app()->request->getPost('param2');


        $this->render('newView', array(

            'param1' => $param1,

            'param2' => $param2,

        ));

    }



I can not seem to access the POST values.

Regards,

Andy.

Yes, you’re calling them correctly. I’d check $_POST directly, most probably it’s empty if you don’t get your parameters.

References to documentation related to this thread:

CHtml.link() Submit and params attributes

CHtml.link()