I am using Yii and I have a form wrapped in tabs. In one of these tabs I need to put a link (call a controller/action
with parameters) independent from the form content. I've tried to use CHtml::linkButton
but it doesn't work.
The code scheme of the form is the following:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'project-form',
'enableAjaxValidation'=>true,
)); ?>
//...input elements
<?php echo CHtml::linkButton('Download',
array(
'submit'=>$this->createUrl('controller/action'),
'params'=>array(
'results'=>CJSON::encode(array('foo'=>'bar'))
))
); ?>
//...other input elements
<?php $this->endWidget(); ?>
When I click on the link nothing happens. If I put the linkButton
code outside the form it works properly.
Is there a workaround for this?
It looks like you're trying to use a button to let the user download some sort of file. For this situation AJAX won't work as it's not capable of saving files to a users computer.
What you want to do is to create just the anchor tag. The function linkButton is designed for is to be a button that will submit the current form. If you're using bootstrap the below will also make it look like a button.
CHtml::link('Download', array('controller/action', 'myparam' => 'paramvalue'), array('class', 'btn btn-primary'));
Make sure you set the correct headers for downloading the file.
In this case you should use ajaxLink
Example:
echo CHtml::ajaxLink(
$text = 'Click me',
$url = '/',
$ajaxOptions=array (
'type'=>'POST',
'dataType'=>'json',
'success'=>'function(html){ jQuery("#your_id").html(html); }'
),
$htmlOptions=array ()
);