[SOLVED] Can CHtml::link post form data?


I have this code in my view:

<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::textField('items_saved',$data->items_saved); ?>

<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

<?php echo CHtml::link(CHtml::encode($data->name),array('showItemsSavedInGrid')); ?>

<br />

<?php echo CHtml::endForm(); ?>   

Now what I want to do is to send [color="#000080"]$data->items_saved[/color] which is string like ‘1000004, 1000006, 1000009’.

So when user clicks name (CHtml:link which I am trying to do) it should call showItemsSavedInGrid() with ‘1000004, 1000006, 1000009’ which I use as [color="#FF0000"]IN[/color] parameter.

showItemsSavedInGrid() looks like:

public function showItemsSavedInGrid()



    $dataProvider=new CActiveDataProvider('AllItems',array(


        // where id IN ('1000004, 1000006, 1000009')

    'condition'=>'id IN ('.$items_saved.'),



        //renders cGridView





And on new view I have rendered cGridView with my condition where id IN (‘1000004, 1000006, 1000009’).

Anyway it seems like $_POST[‘items_saved’] is not sent to controller. Because in application.log I have:

2010/11/22 21:14:22 [error] [system.db.CDbCommand] 

Error in querying SQL: 

SELECT COUNT(*) FROM `AllItems` `t` WHERE id IN ()

Can I send POST data by CHtml::link? Or maybe there is other way to send string like ‘1000004, 1000006, 1000009’ by CHtml::link to the controller.

Thanks for any suggestions.


<?php echo CHtml::link(CHtml::encode($data->name),array('showItemsSavedInGrid',array('items_saved'=>$data->items_saved))); ?>

and in controller access via


Yeah that works as I expected

When I changed to $_GET, the URL in address bar is like:


Anyway I wanted to use $_POST not to have this IDs in URL. IS that possible?

If you wish to do that I recommend that you write your onclick javascript procedure to actually call a function that posts a dummy form.

It worked for me long time a go without knowing Yii… so what stops this technique to work with your approach now?

OK I’ll try that javascript thing.

You may use CHtml.linkButton to submit form on link is clicked.

Can I call any controller method using CHtml::linkButton ?

public static string linkButton(string $label='submit', array $htmlOptions=array ( ))


Yes you can:

echo CHtml::linkButton('Submit form', array('submit' => array('route')));

But you may also set this in form’s action directly.

I will create a button (not submit) or a link, that is up to you…

In your HTML create a dummy form:

<form id="dummy" action="" method="post">

<input type="hidden" name="variablenamehere" id="dummy-field"/>


Now, the important part is in your htmlOptions of the link or button or whatever you want that holds an onclick




  &#036;(&quot;dummy-form&quot;).submit();return false;'

return false is if you do it from a dummy link (i.e. href="#") otherwise it will go to the top of the page. Now, with the same technique you could provide the with a class name, and also have the meta data to be posted in an attribute, then ON DOCUMENT READY, use jquery to update the dummy field and submit the form.


   var dummyField = $(this).attr("rel");


   $("dummy-form").submit();return false;' 


Hi, I’ve been struggling with this all day, and what I found in this thread is technically what I need but can make it work ;(

I need to create a link with GET parameters the same way a form does.

I’ve written this:

<?php echo CHtml::link('test',array('yacht/index',array('SearchForm'=>array('area'=>'1','location'=>'src3')))); ?>

Similar to what rudenich suggested:

<?php echo CHtml::link(CHtml::encode($data->name),array('showItemsSavedInGrid',array('items_saved'=>$data->items_saved))); ?>

It creates the following URL: http://localhost:8888/www7/yacht/index?0[SearchForm][area]=1&0[SearchForm][location]=src3



has no value at all.

I don’t know why it adds ‘[’ and ‘]’ to SearchForm… when a form action generates a url it does it like this:


Which works because


gets populated…

Any suggestion???


Two possible solutions:

<?php echo CHtml::link('test',array('yacht/index','SearchForm'=>array('area'=>'1','location'=>'src3'))); ?>

<?php echo CHtml::link('test', Yii::app()->createUrl('yacht/index',array('SearchForm'=>array('area'=>'1','location'=>'src3')))); ?>